예제 #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));
        }