コード例 #1
0
        private void InitializeIdentityForEF(AppUserIdentityDbContext context)
        {
            var    UserManager = new UserManager <CustUserIdentity, int>(new UserStoreInt(context));
            var    RoleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            string name        = "test";
            string password    = "******";

            //Create Role Test and User Test

            UserManager.Create(new CustUserIdentity()
            {
                UserName  = name,
                Domain    = "ITEEDEE",
                FirstName = "Test User"
            }, password);

            //Create Role Admin if it does not exist


            //Create User=Admin with password=123456
            var user = new CustUserIdentity();

            user.UserName  = name;
            user.Domain    = "ITEEDEE";
            user.FirstName = "Admin User";
            var adminresult = UserManager.CreateAsync(user);

            //Add User Admin to Role Admin
            if (adminresult.IsCompleted)
            {
                var result = UserManager.AddToRoleAsync(user.Id, name);
            }
        }
コード例 #2
0
        private async Task SignInAsync(CustUserIdentity user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
            var identity = await CustomUserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

            AuthenticationManager.SignIn(new AuthenticationProperties {
                IsPersistent = isPersistent
            }, identity);
        }
コード例 #3
0
        private async Task <SignInStatus> SignInOrTwoFactor(CustUserIdentity user, bool isPersistent)
        {
            if (await CustomUserManager.GetTwoFactorEnabledAsync(user.Id) &&
                !await AuthenticationManager.TwoFactorBrowserRememberedAsync(user.Id.ToString()))
            {
                var identity = new ClaimsIdentity(DefaultAuthenticationTypes.TwoFactorCookie);
                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
                identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
                AuthenticationManager.SignIn(identity);
                return(SignInStatus.RequiresTwoFactorAuthentication);
            }
            await SignInAsync(user, isPersistent, false);

            return(SignInStatus.Success);
        }
コード例 #4
0
        public async Task SignInAsync(CustUserIdentity user, bool isPersistent, bool rememberBrowser)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
            var userIdentity = await user.GenerateUserIdentityAsync(CustomUserManager);

            if (rememberBrowser)
            {
                var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(user.Id.ToString());
                AuthenticationManager.SignIn(new AuthenticationProperties {
                    IsPersistent = isPersistent
                }, userIdentity, rememberBrowserIdentity);
            }
            else
            {
                AuthenticationManager.SignIn(new AuthenticationProperties {
                    IsPersistent = isPersistent
                }, userIdentity);
            }
        }
コード例 #5
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                //register the user
                try
                {
                    if (ModelState.IsValid)
                    {
                        var user = new CustUserIdentity {
                            UserName = model.UserName
                        };
                        var result = await CustomUserManager.CreateAsync(user, model.Password);

                        if (result.Succeeded)
                        {
                            await SignInAsync(user, isPersistent : false);

                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            AddErrors(result);
                        }
                    }


                    return(View(model));
                }
                catch (Exception e)
                {
                    //ModelState.AddModelError();
                }
            }

            return(View(model));
        }