protected virtual async Task <IEnumerable <Claim> > GetClaimsFromAccount(TUser user) { var claims = new List <Claim> { new Claim(Constants.ClaimTypes.Subject, user.Id.ToString()), new Claim(Constants.ClaimTypes.PreferredUserName, user.UserName), }; if (userManager.SupportsUserEmail) { var email = await userManager.GetEmailAsync(user.Id); if (!String.IsNullOrWhiteSpace(email)) { claims.Add(new Claim(Constants.ClaimTypes.Email, email)); var verified = await userManager.IsEmailConfirmedAsync(user.Id); claims.Add(new Claim(Constants.ClaimTypes.EmailVerified, verified ? "true" : "false")); } } if (userManager.SupportsUserPhoneNumber) { var phone = await userManager.GetPhoneNumberAsync(user.Id); if (!String.IsNullOrWhiteSpace(phone)) { claims.Add(new Claim(Constants.ClaimTypes.PhoneNumber, phone)); var verified = await userManager.IsPhoneNumberConfirmedAsync(user.Id); claims.Add(new Claim(Constants.ClaimTypes.PhoneNumberVerified, verified ? "true" : "false")); } } if (userManager.SupportsUserClaim) { claims.AddRange(await userManager.GetClaimsAsync(user.Id)); } if (userManager.SupportsUserRole) { var roleClaims = from role in await userManager.GetRolesAsync(user.Id) select new Claim(Constants.ClaimTypes.Role, role); claims.AddRange(roleClaims); } return(claims); }
public async Task <IActionResult> SendCode(SendCodeViewModel model) { if (!ModelState.IsValid) { return(View()); } var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { return(View("Error")); } // Generate the token and send it var code = await _userManager.GenerateTwoFactorTokenAsync(user, model.SelectedProvider); if (string.IsNullOrWhiteSpace(code)) { return(View("Error")); } var message = "Your security code is: " + code; if (model.SelectedProvider == "Email") { await _emailSender.SendEmailAsync(await _userManager.GetEmailAsync(user), "Security Code", message); } else if (model.SelectedProvider == "Phone") { await _smsSender.SendSmsAsync(await _userManager.GetPhoneNumberAsync(user), message); } return(RedirectToAction(nameof(VerifyCode), new { Provider = model.SelectedProvider, ReturnUrl = model.ReturnUrl, RememberMe = model.RememberMe })); }