/// <summary> /// Signs in a user by username and password + confirms the identity. /// </summary> /// <typeparam name="TUser"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="signInManager"></param> /// <param name="userName"></param> /// <param name="password"></param> /// <param name="isPersistent"></param> /// <param name="shouldLockout"></param> /// <returns></returns> public static async Task <SignInStatus> PasswordSignInAndConfirmAsync <TUser, TKey>(this SignInManager <TUser, TKey> signInManager, string userName, string password, bool isPersistent, bool shouldLockout) where TUser : class, IUser <TKey> where TKey : IEquatable <TKey>, IConvertible { SignInStatus status = await signInManager.PasswordSignInAsync(userName, password, isPersistent, shouldLockout); if (status == SignInStatus.Success) { // We have to override the existing grant with a new one, as we are adding // a new claim TUser user = await signInManager.UserManager.FindByNameAsync(userName); string userIdString = signInManager.ConvertIdToString(user.Id); AuthenticationResponseGrant grant = signInManager.AuthenticationManager.AuthenticationResponseGrant; grant.Identity.AddClaim(new Claim(DoubleConfirmIdentityConstants.ClaimType, userIdString)); signInManager.AuthenticationManager.SignIn(grant.Properties, grant.Identity); } return(status); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, First_Name = model.First_Name, Last_Name = model.Last_Name, Address = model.Address }; // System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage( //new System.Net.Mail.MailAddress("*****@*****.**", "Web Registration"), //new System.Net.Mail.MailAddress(user.Email)); // m.Subject = "Email confirmation"; // m.Body = string.Format("Dear {0}< BR /> Thank you for your registration, please click on the below link to complete your registration: < a href =\"{1}\"title =\"User Email Confirm\">{1}</a>", // user.UserName, Url.Action("ConfirmEmail", "Account", // new { Token = user.Id, Email = user.Email }, Request.Url.Scheme)) ; // m.IsBodyHtml = true; // System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("firstvmtle.southeastasia.cloudapp.azure.com"); // smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "password@1234");\ // smtp.EnableSsl = true; // smtp.Send(m); var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { string tt = SignInManager.ConvertIdToString(user.Id); //await UserManager.AddToRoleAsync(user.Id, "admin"); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit https://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")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
/// <summary> /// Confirms the given identity /// </summary> /// <typeparam name="TUser"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="signInManager"></param> /// <param name="identity"></param> /// <returns></returns> public static async Task <SignInStatus> DoubleConfirmIdentityAsync <TUser, TKey>(this SignInManager <TUser, TKey> signInManager, ClaimsIdentity identity) where TUser : class, IUser <TKey> where TKey : IEquatable <TKey>, IConvertible { SignInStatus status; if (identity == null || signInManager.UserManager == null) { status = SignInStatus.Failure; } else { TKey userId = identity.GetUserId <TKey>(); TUser user = await signInManager.UserManager.FindByIdAsync(userId); if (user == null) { status = SignInStatus.Failure; } else { bool isLockedOut = await signInManager.UserManager.IsLockedOutAsync(userId); if (isLockedOut) { status = SignInStatus.LockedOut; } else { string userIdString = signInManager.ConvertIdToString(userId); ClaimsIdentity newIdentity = new ClaimsIdentity(identity); newIdentity.AddClaim(new Claim(DoubleConfirmIdentityConstants.ClaimType, userIdString)); signInManager.AuthenticationManager.SignIn(newIdentity); status = SignInStatus.Success; } } } return(status); }