public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new AzureIdentityUser() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent : false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("Index", "Home")); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return(View(model)); }
private async Task SignInAsync(AzureIdentityUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, user.GenerateUserIdentityAsync(UserManager, user)); }
public async Task <ActionResult> ExternalLoginCallback(string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return(RedirectToAction("Login", new { returnUrl = returnUrl })); } // Sign in the user with this external login provider if the user already has a login var user = await UserManager.FindAsync(loginInfo.Login); if (user != null) { user.Claims = loginInfo.ExternalIdentity.Claims.ToList(); await SignInAsync(user, isPersistent : false); } else { // If the user does not have an account, then create an account user = new AzureIdentityUser() { UserName = loginInfo.Email, Email = loginInfo.Email, Claims = loginInfo.ExternalIdentity.Claims.ToList() }; IdentityResult result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent : false); } } } Uri uri = null; if (string.IsNullOrEmpty(returnUrl) == false) { uri = new Uri(returnUrl); } JwtTokenHelper jwtTokenHelper = new JwtTokenHelper(); List <Claim> claims = new List <Claim>(); foreach (Claim claim in loginInfo.ExternalIdentity.Claims) { switch (claim.Type) { case ClaimTypes.Email: claims.Add(claim); break; case ClaimTypes.GivenName: claims.Add(claim); break; case ClaimTypes.Surname: claims.Add(claim); break; case ClaimTypes.Expiration: claims.Add(claim); break; case "urn:linkedin:accesstoken": claims.Add(claim); break; default: break; } } Claim expClaim = claims.Where(c => c.Type == ClaimTypes.Expiration).FirstOrDefault(); DateTime dExpires = DateTime.Now.AddHours(12); TimeSpan?expires = null; if (expClaim != null) { expires = TimeSpan.Parse(expClaim.Value); if (expires.HasValue) { dExpires = DateTime.Now.Add(expires.Value); } } string jwtToken = jwtTokenHelper.GenerateJwtToken(loginInfo.Login.LoginProvider, uri?.Host, claims, null, dExpires); UriBuilder uriBuilder = new UriBuilder(returnUrl); NameValueCollection queryCollection = HttpUtility.ParseQueryString(uriBuilder.Query); queryCollection["access_token"] = jwtToken; uriBuilder.Query = queryCollection.ToString(); return(RedirectToAction("OAuth2Confirmation", new { returnUrl = uriBuilder.ToString() })); }