示例#1
0
        public async Task<IdentityResult> RegisterCustomerAsync(UserModel userModel, AppRole appRole = AppRole.customer)
        {
            var addToRoleResult = new IdentityResult();
            // Add generate username to the model
            IdentityUser user = new IdentityUser
            {
                UserName = userModel.UserName,
                Email = userModel.EmailAddress,
                EmailConfirmed = true,
                PhoneNumber = userModel.PhoneNumber,
                PhoneNumberConfirmed = true,
            };

            var result = await _userManager.CreateAsync(user, userModel.Password);

            //Role-user combination is managed as Contact-Role combination in ContactRole table
            //Create Role Admin if it does not exist
            var _role = await _roleManager.FindByNameAsync(appRole.ToString());
            if (_role == null)
            {
                _role = new IdentityRole(appRole.ToString());
                var roleresult = await _roleManager.CreateAsync(_role);
            }
            var rolesForUser = await _userManager.GetRolesAsync(user.Id);
            if (!rolesForUser.Contains(_role.Name))
            {
                addToRoleResult = await _userManager.AddToRoleAsync(user.Id, _role.Name);
            }

            return addToRoleResult;
        }
        /// <summary>
        /// Seeds the asynchronous.
        /// </summary>
        /// <param name="userManager">The user manager.</param>
        /// <param name="roleManager">The role manager.</param>
        /// <returns></returns>
        public static async Task SeedAsync(UserManager <ApplicationUser> userManager, RoleManager <AppRole> roleManager)
        {
            var defaultRole1 = new AppRole {
                Name = "System Administrator"
            };
            var defaultRole2 = new AppRole {
                Name = "Culture Object Administrator"
            };
            var defaultRole3 = new AppRole {
                Name = "Fun Zone Administrator"
            };
            var defaultRole4 = new AppRole {
                Name = "Registered User"
            };
            var defaultRole5 = new AppRole {
                Name = "Unregistered User"
            };

            var defaultUser1 = new ApplicationUser {
                UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, PhoneNumberConfirmed = true
            };
            var defaultUser2 = new ApplicationUser {
                UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, PhoneNumberConfirmed = true
            };
            var defaultUser3 = new ApplicationUser {
                UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, PhoneNumberConfirmed = true
            };
            var defaultUser4 = new ApplicationUser {
                UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, PhoneNumberConfirmed = true
            };

            await roleManager.CreateAsync(defaultRole1);

            await roleManager.CreateAsync(defaultRole2);

            await roleManager.CreateAsync(defaultRole3);

            await roleManager.CreateAsync(defaultRole4);

            await roleManager.CreateAsync(defaultRole5);

            await userManager.CreateAsync(defaultUser1, "Password1");

            await userManager.AddToRoleAsync(defaultUser1, defaultRole1.ToString());

            await userManager.CreateAsync(defaultUser2, "Password2");

            await userManager.AddToRoleAsync(defaultUser2, defaultRole2.ToString());

            await userManager.CreateAsync(defaultUser3, "Password3");

            await userManager.AddToRoleAsync(defaultUser3, defaultRole3.ToString());

            await userManager.CreateAsync(defaultUser4, "Password4");

            await userManager.AddToRoleAsync(defaultUser4, defaultRole4.ToString());
        }
        public static bool IsInRole(this IPrincipal user, AppRole appRole)
        {
            var roles = appRole.ToString().Split(',').Select(x => x.Trim());
            foreach (var role in roles)
            {
                if (user.IsInRole(role))
                    return true;
            }

            return false;
        }
        public static bool IsInRole(this IPrincipal user, AppRole appRole)
        {
            var roles = appRole.ToString().Split(',').Select(x => x.Trim());

            foreach (var role in roles)
            {
                if (user.IsInRole(role))
                {
                    return(true);
                }
            }

            return(false);
        }
        private async Task <AuthenticateResult> ManagerAuthorization(AppRole enumRole)
        {
            Manager user = null;

            try
            {
                var authHeader      = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
                var credentials     = Encoding.UTF8.GetString(credentialBytes).Split(new[] { ':' }, 2);
                var username        = credentials[0];

                var query = new GetUserQuery <Manager>()
                {
                    Login = username
                };

                user = await queryExecutor.Execute(query);

                if (user == null)
                {
                    return(AuthenticateResult.Fail("Resource does not exist"));
                }

                var password = passwordHasher.HashToCheck(credentials[1], user.Salt);

                if (user.Password != password)
                {
                    return(AuthenticateResult.Fail("Wrong password"));
                }
            }
            catch
            {
                return(AuthenticateResult.Fail("Invalid Authorization Header"));
            }

            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Name, user.Login),
                new Claim(ClaimTypes.Role, enumRole.ToString()),
                new Claim(ClaimTypes.UserData, user.Company.Id.ToString())
            };

            var identity  = new ClaimsIdentity(claims, Scheme.Name);
            var principal = new ClaimsPrincipal(identity);
            var ticket    = new AuthenticationTicket(principal, Scheme.Name);

            return(AuthenticateResult.Success(ticket));
        }