Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // POST: /Account/ExternalLogin
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public ActionResult ExternalLogin(string provider, string returnUrl)
- {
- return new ExternalLoginResult(provider, Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
- }
- //
- // GET: /Account/ExternalLoginCallback
- [AllowAnonymous]
- public ActionResult ExternalLoginCallback(string returnUrl)
- {
- AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
- if (!result.IsSuccessful)
- return RedirectToAction("ExternalLoginFailure");
- if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
- return RedirectToLocal(returnUrl);
- if (User.Identity.IsAuthenticated)
- {
- // If the current user is logged in add the new account
- OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
- return RedirectToLocal(returnUrl);
- }
- else
- {
- // User is new, ask for their desired membership name
- string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
- var data = OAuthWebSecurity.GetOAuthClientData(result.Provider);
- ViewBag.ProviderDisplayName = data.DisplayName;
- ViewBag.ReturnUrl = returnUrl;
- return View("ExternalLoginConfirmation", new RegisterExternalLoginModel { UserName = result.UserName, ExternalLoginData = loginData });
- }
- }
- //
- // POST: /Account/ExternalLoginConfirmation
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
- {
- string provider = null;
- string providerUserId = null;
- if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
- {
- return RedirectToAction("Manage");
- }
- if (ModelState.IsValid)
- {
- // Insert a new user into the database
- using (Context db = new Context())
- {
- UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
- // Check if user already exists
- if (user == null)
- {
- // Insert name into the profile table
- db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
- db.SaveChanges();
- OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
- OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
- return RedirectToLocal(returnUrl);
- }
- else
- {
- ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
- }
- }
- }
- ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
- ViewBag.ReturnUrl = returnUrl;
- return View(model);
- }
- //
- // GET: /Account/ExternalLoginFailure
- [AllowAnonymous]
- public ActionResult ExternalLoginFailure()
- {
- return View();
- }
- [AllowAnonymous]
- [ChildActionOnly]
- public ActionResult ExternalLoginsList(string returnUrl)
- {
- ViewBag.ReturnUrl = returnUrl;
- return PartialView("_ExternalLoginsListPartial", OAuthWebSecurity.RegisteredClientData);
- }
- [ChildActionOnly]
- public ActionResult RemoveExternalLogins()
- {
- ICollection<OAuthAccount> accounts = OAuthWebSecurity.GetAccountsFromUserName(User.Identity.Name);
- List<ExternalLogin> externalLogins = new List<ExternalLogin>();
- foreach (OAuthAccount account in accounts)
- {
- AuthenticationClientData clientData = OAuthWebSecurity.GetOAuthClientData(account.Provider);
- externalLogins.Add(new ExternalLogin
- {
- Provider = account.Provider,
- ProviderDisplayName = clientData.DisplayName,
- ProviderUserId = account.ProviderUserId,
- });
- }
- ViewBag.ShowRemoveButton = externalLogins.Count > 1 || OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
- return PartialView("_RemoveExternalLoginsPartial", externalLogins);
- }
- internal class ExternalLoginResult : ActionResult
- {
- public ExternalLoginResult(string provider, string returnUrl)
- {
- Provider = provider;
- ReturnUrl = returnUrl;
- }
- public string Provider { get; private set; }
- public string ReturnUrl { get; private set; }
- public override void ExecuteResult(ControllerContext context)
- {
- OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment