Пример #1
0
        private static IdentityUser AddAdmin(UserManager <IdentityUser> userMgr)
        {
            var admin = userMgr.FindByNameAsync(AdminEmail).Result;

            if (admin == null)
            {
                admin = new IdentityUser
                {
                    UserName       = AdminEmail,
                    Email          = AdminEmail,
                    EmailConfirmed = true
                };
                var result = userMgr.CreateAsync(admin, "admin").Result;
                if (!result.Succeeded)
                {
                    throw new Exception(result.Errors.First().Description);
                }

                result = userMgr.AddClaimsAsync(admin, new Claim[]
                {
                    new Claim(JwtClaimTypes.Name, "Admin CoolTool"),
                    new Claim(JwtClaimTypes.Email, AdminEmail),
                    new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean),
                    new Claim("action", "action1"),
                    new Claim("action", "action2")
                }).Result;
                if (!result.Succeeded)
                {
                    throw new Exception(result.Errors.First().Description);
                }
            }
            return(userMgr.FindByNameAsync(AdminEmail).Result);
        }
Пример #2
0
        private async Task <IdentityUser> AutoProvisionUserAsync(string provider, string providerUserId, IEnumerable <Claim> claims)
        {
            // create a list of claims that we want to transfer into our store
            var filtered = new List <Claim>();
            // user's display name
            var enumerableClaims = claims as Claim[] ?? claims.ToArray();
            var name             = enumerableClaims.FirstOrDefault(x => x.Type == JwtClaimTypes.Name)?.Value ??
                                   enumerableClaims.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value;

            if (name != null)
            {
                filtered.Add(new Claim(JwtClaimTypes.Name, name));
            }
            else
            {
                var first = enumerableClaims.FirstOrDefault(x => x.Type == JwtClaimTypes.GivenName)?.Value ??
                            enumerableClaims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value;
                var last = enumerableClaims.FirstOrDefault(x => x.Type == JwtClaimTypes.FamilyName)?.Value ??
                           enumerableClaims.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value;
                if (first != null && last != null)
                {
                    filtered.Add(new Claim(JwtClaimTypes.Name, first + " " + last));
                }
                else if (first != null)
                {
                    filtered.Add(new Claim(JwtClaimTypes.Name, first));
                }
                else if (last != null)
                {
                    filtered.Add(new Claim(JwtClaimTypes.Name, last));
                }
            }
            var email = enumerableClaims.FirstOrDefault(x => x.Type == JwtClaimTypes.Email)?.Value ??
                        enumerableClaims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value;

            if (email != null)
            {
                filtered.Add(new Claim(JwtClaimTypes.Email, email));
            }
            var user = new IdentityUser
            {
                UserName = email,
                Email    = email
            };
            var identityResult = await _UserManager.CreateAsync(user);

            if (!identityResult.Succeeded)
            {
                throw new Exception(identityResult.Errors.First().Description);
            }
            if (filtered.Any())
            {
                identityResult = await _UserManager.AddClaimsAsync(user, filtered);

                if (!identityResult.Succeeded)
                {
                    throw new Exception(identityResult.Errors.First().Description);
                }
            }
            identityResult = await _UserManager.AddLoginAsync(user, new UserLoginInfo(provider, providerUserId, provider));

            if (!identityResult.Succeeded)
            {
                throw new Exception(identityResult.Errors.First().Description);
            }
            return(user);
        }