public async Task <bool> SeedAdministratorRolesAndUser() { var roleManager = new RedfernRoleManager(new Microsoft.AspNet.Identity.EntityFramework.RoleStore <Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RedfernSecurityContext())); roleManager.SeedRoles(); var exists = await roleManager.RoleExistsAsync("ADMIN"); if (!exists) { await roleManager.CreateAsync(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole("ADMIN")); } // create an administrator user with a default password var userManager = new RedfernUserManager(new Microsoft.AspNet.Identity.EntityFramework.UserStore <RedfernUser>(new RedfernSecurityContext())); var user = await userManager.FindByNameAsync("Administrator"); if (user == null) { user = new RedfernUser { UserName = "******", FullName = "Administrator", Email = "*****@*****.**", IsEnabled = true, SignupDate = DateTime.UtcNow, TenantId = 1 }; await userManager.CreateAsync(user, "Password1"); await userManager.AddToRoleAsync(user.Id, "ADMIN"); } return(true); }
private async Task SignInAsync(RedfernUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new RedfernUser() { UserName = model.UserName.ToLower(), FullName = model.FullName, Email = model.Email, TenantId = _context.TenantID, SignupDate = DateTime.UtcNow, IsEnabled = true, EmailConfirmed = false }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // send a confirmation email var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); EmailConfirmationEmail email = new EmailConfirmationEmail(); email.To = user.Email; email.UserFullName = user.FullName; email.ConfirmEmailLink = callbackUrl; email.Code = code; await email.SendAsync(); // send an email to the admin account that a new user has registered if (!String.IsNullOrEmpty(AppSettings.AdminEmail)) { NewUserRegistrationEmail registrationEmail = new NewUserRegistrationEmail(); registrationEmail.To = AppSettings.AdminEmail; registrationEmail.NewUserName = user.UserName; registrationEmail.NewUserFullName = user.FullName; registrationEmail.NewUserEmailAddress = user.Email; await registrationEmail.SendAsync(); } ViewBag.UserEmail = user.Email; return(View("RegisterSuccess")); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return(View(model)); }
private async Task <RSignInStatus> SignInOrTwoFactor(RedfernUser user, bool isPersistent) { if (await UserManager.GetTwoFactorEnabledAsync(user.Id) && !await AuthenticationManager.TwoFactorBrowserRememberedAsync(user.Id)) { var identity = new ClaimsIdentity(DefaultAuthenticationTypes.TwoFactorCookie); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id)); AuthenticationManager.SignIn(identity); return(RSignInStatus.RequiresTwoFactorAuthentication); } await SignInAsync(user, isPersistent, false); return(RSignInStatus.Success); }
public async Task SignInAsync(RedfernUser user, bool isPersistent, bool rememberBrowser) { // Clear any partial cookies from external or two factor partial sign ins AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie); var userIdentity = await user.GenerateUserIdentityAsync(UserManager); if (rememberBrowser) { var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(user.Id); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity, rememberBrowserIdentity); } else { AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity); } }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Manage")); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } var user = new RedfernUser() { UserName = model.UserName }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent : false); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }