Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2013
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using Microsoft.AspNet.Identity;
  2. using Microsoft.AspNet.Identity.EntityFramework;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using Microsoft.Owin.Security;
  10.  
  11. namespace Spoilerfy.Controllers
  12. {
  13.     public class AccountController : Controller
  14.     {
  15.         private IAuthenticationManager AuthenticationManager
  16.         {
  17.             get
  18.             {
  19.                 return HttpContext.GetOwinContext().Authentication;
  20.             }
  21.         }
  22.  
  23.         public ActionResult Index()
  24.         {
  25.             return View();
  26.         }
  27.  
  28.         [HttpPost]
  29.         [AllowAnonymous]
  30.         [ValidateAntiForgeryToken]
  31.         public ActionResult ExternalLogin(string provider, string returnUrl)
  32.         {
  33.             // Request a redirect to the external login provider
  34.             return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
  35.         }
  36.  
  37.         [AllowAnonymous]
  38.         public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
  39.         {
  40.             var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
  41.             var falseNotAuthenticated = AuthenticationManager.User.Identity.IsAuthenticated;
  42.  
  43.             if (loginInfo == null)
  44.             {
  45.                 return RedirectToAction("Login");
  46.             }
  47.  
  48.             return RedirectToAction("Index", "Home");
  49.         }
  50.     }
  51.  
  52.         private class ChallengeResult : HttpUnauthorizedResult
  53.         {
  54.             public ChallengeResult(string provider, string redirectUri) : this(provider, redirectUri, null)
  55.             {
  56.             }
  57.  
  58.             public ChallengeResult(string provider, string redirectUri, string userId)
  59.             {
  60.                 LoginProvider = provider;
  61.                 RedirectUri = redirectUri;
  62.                 UserId = userId;
  63.             }
  64.  
  65.             public string LoginProvider { get; set; }
  66.             public string RedirectUri { get; set; }
  67.             public string UserId { get; set; }
  68.  
  69.             public override void ExecuteResult(ControllerContext context)
  70.             {
  71.                 var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
  72.                 if (UserId != null)
  73.                 {
  74.                     properties.Dictionary[XsrfKey] = UserId;
  75.                 }
  76.                 context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
  77.             }
  78.         }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement