Shimmy

OpenAuth

Apr 7th, 2013
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.96 KB | None | 0 0
  1.     // POST: /Account/ExternalLogin  
  2.     [HttpPost]
  3.     [AllowAnonymous]
  4.     [ValidateAntiForgeryToken]
  5.     public ActionResult ExternalLogin(string provider, string returnUrl)
  6.     {
  7.       return new ExternalLoginResult(provider, Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
  8.     }
  9.  
  10.     //
  11.     // GET: /Account/ExternalLoginCallback
  12.  
  13.     [AllowAnonymous]
  14.     public ActionResult ExternalLoginCallback(string returnUrl)
  15.     {
  16.       AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
  17.       if (!result.IsSuccessful)
  18.         return RedirectToAction("ExternalLoginFailure");
  19.  
  20.       if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
  21.         return RedirectToLocal(returnUrl);
  22.  
  23.       if (User.Identity.IsAuthenticated)
  24.       {
  25.         // If the current user is logged in add the new account
  26.         OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
  27.         return RedirectToLocal(returnUrl);
  28.       }
  29.       else
  30.       {
  31.         // User is new, ask for their desired membership name
  32.         string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
  33.         var data = OAuthWebSecurity.GetOAuthClientData(result.Provider);
  34.  
  35.  
  36.  
  37.         ViewBag.ProviderDisplayName = data.DisplayName;
  38.         ViewBag.ReturnUrl = returnUrl;
  39.         return View("ExternalLoginConfirmation", new RegisterExternalLoginModel { UserName = result.UserName, ExternalLoginData = loginData });
  40.       }
  41.     }
  42.  
  43.     //
  44.     // POST: /Account/ExternalLoginConfirmation
  45.  
  46.     [HttpPost]
  47.     [AllowAnonymous]
  48.     [ValidateAntiForgeryToken]
  49.     public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
  50.     {
  51.       string provider = null;
  52.       string providerUserId = null;
  53.  
  54.       if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
  55.       {
  56.         return RedirectToAction("Manage");
  57.       }
  58.  
  59.       if (ModelState.IsValid)
  60.       {
  61.         // Insert a new user into the database
  62.         using (Context db = new Context())
  63.         {
  64.           UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
  65.           // Check if user already exists
  66.           if (user == null)
  67.           {
  68.             // Insert name into the profile table
  69.             db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
  70.             db.SaveChanges();
  71.  
  72.             OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
  73.             OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
  74.  
  75.             return RedirectToLocal(returnUrl);
  76.           }
  77.           else
  78.           {
  79.             ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
  80.           }
  81.         }
  82.       }
  83.  
  84.       ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
  85.       ViewBag.ReturnUrl = returnUrl;
  86.       return View(model);
  87.     }
  88.  
  89.     //
  90.     // GET: /Account/ExternalLoginFailure
  91.  
  92.     [AllowAnonymous]
  93.     public ActionResult ExternalLoginFailure()
  94.     {
  95.       return View();
  96.     }
  97.  
  98.     [AllowAnonymous]
  99.     [ChildActionOnly]
  100.     public ActionResult ExternalLoginsList(string returnUrl)
  101.     {
  102.       ViewBag.ReturnUrl = returnUrl;
  103.       return PartialView("_ExternalLoginsListPartial", OAuthWebSecurity.RegisteredClientData);
  104.     }
  105.  
  106.     [ChildActionOnly]
  107.     public ActionResult RemoveExternalLogins()
  108.     {
  109.       ICollection<OAuthAccount> accounts = OAuthWebSecurity.GetAccountsFromUserName(User.Identity.Name);
  110.       List<ExternalLogin> externalLogins = new List<ExternalLogin>();
  111.       foreach (OAuthAccount account in accounts)
  112.       {
  113.         AuthenticationClientData clientData = OAuthWebSecurity.GetOAuthClientData(account.Provider);
  114.  
  115.         externalLogins.Add(new ExternalLogin
  116.         {
  117.           Provider = account.Provider,
  118.           ProviderDisplayName = clientData.DisplayName,
  119.           ProviderUserId = account.ProviderUserId,
  120.         });
  121.       }
  122.  
  123.       ViewBag.ShowRemoveButton = externalLogins.Count > 1 || OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
  124.       return PartialView("_RemoveExternalLoginsPartial", externalLogins);
  125.     }
  126.  
  127.  
  128.     internal class ExternalLoginResult : ActionResult
  129.     {
  130.       public ExternalLoginResult(string provider, string returnUrl)
  131.       {
  132.         Provider = provider;
  133.         ReturnUrl = returnUrl;
  134.       }
  135.  
  136.       public string Provider { get; private set; }
  137.       public string ReturnUrl { get; private set; }
  138.  
  139.       public override void ExecuteResult(ControllerContext context)
  140.       {
  141.         OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl);
  142.       }
  143.     }
Advertisement
Add Comment
Please, Sign In to add comment