示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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));
        }
示例#4
0
        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);
        }
示例#5
0
        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);
            }
        }
示例#6
0
        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));
        }