コード例 #1
0
        public static void EnsureSeedData(this IdentityServerContext context)
        {
            var adminRoleId = Guid.NewGuid();

            if (!context.Profiles.Any())
            {
                context.Profiles.AddRange(
                    new Profile[] {
                    new Profile
                    {
                        Name             = AccountProfiles.ADMIN,
                        NormalizedName   = AccountProfiles.ADMIN,
                        Id               = adminRoleId,
                        ConcurrencyStamp = adminRoleId.ToString()
                    },
                    new Profile
                    {
                        Name             = AccountProfiles.COMMON_USER,
                        NormalizedName   = AccountProfiles.COMMON_USER,
                        Id               = Guid.NewGuid(),
                        ConcurrencyStamp = Guid.NewGuid().ToString()
                    },
                }
                    );
                context.SaveChanges();
            }

            if (!context.Accounts.Any())
            {
                var user = new Account("admin", "*****@*****.**");

                var pwHasher = new PasswordHasher <Account>();
                user.PasswordHash   = pwHasher.HashPassword(user, "4adm1ns3rver");
                user.EmailConfirmed = true;

                context.Accounts.Add(user);

                context.UserRoles.Add(
                    new IdentityUserRole <Guid>
                {
                    RoleId = adminRoleId,
                    UserId = user.Id
                }
                    );
                context.SaveChanges();
            }
        }
コード例 #2
0
        public Account SignUp(Account account)
        {
            CheckAccountIsNull(account);
            var exists = _context.Accounts.FirstOrDefault(x =>
                                                          x.Username == account.Username || x.UserMail == account.UserMail);

            if (exists != null)
            {
                throw new InvalidOperationException(nameof(account.Username) + " or " + nameof(account.UserMail) +
                                                    " already used");
            }
            account = GenerateSaltAndHashPassword(account);
            account = GenerateActivationToken(account);

            _context.Set <Account>().Add(account);
            _context.SaveChanges();
            return(account);
        }