Beispiel #1
0
        public async Task <ClaimsIdentity> SignupUser(ISignupServiceOptions options)
        {
            UserProviderLocal login = await(from p in this.db.LocalProvider.Include(o => o.User)
                                            where p.User.Username == options.Username
                                            select p).FirstOrDefaultAsync();

            if (login != null)
            {
                throw new ServiceException($"A user account for {options.Username} already exists");
            }

            User user = new User()
            {
                CultureName = options.CultureName,
                Enabled     = true,
                Username    = options.Username,
                DisplayName = options.DisplayName,
                TimeZoneId  = options.TimeZoneId
            };

            db.User.Add(user);

            string salt = crypto.CreateSalt();

            db.LocalProvider.Add(new UserProviderLocal()
            {
                PasswordSalt = salt,
                PasswordHash = crypto.CreateKey(salt, options.Password),
                User         = user,
                Provider     = db.Provider.FirstOrDefault(o => o.ProviderId == ProviderTypes.Local)
            });

            Role role = db.Role.FirstOrDefault(o => o.RoleId == RoleTypes.User);

            user.Roles.Add(new UserRole()
            {
                User = user,
                Role = role
            });

            db.SaveChanges();

            var fingerprint = this.deviceProfiler.DeriveFingerprint(user);

            return(user.ToClaimsIdentity(fingerprint));
        }
Beispiel #2
0
        private static User EnsureAdmin(DbContextBase db, ICryptoService crypto)
        {
            User adminUser = db.User.SingleOrDefault(o => o.Username == AdminEmail);

            if (adminUser == null)
            {
                adminUser = new User()
                {
                    Username    = AdminEmail,
                    Enabled     = true,
                    DisplayName = "Webmaster",
                    Verified    = true
                };

                db.User.Add(adminUser);
                db.SaveChanges();
            }

            Role adminRole = db.Role.FirstOrDefault(o => o.RoleId == RoleTypes.Admin);

            if (adminRole == null)
            {
                adminRole = new Role()
                {
                    CreatedByUser = adminUser,
                    Enabled       = true,
                    Name          = "Administrator",
                    RoleId        = RoleTypes.Admin
                };
                db.Role.Add(adminRole);
                db.SaveChanges();
            }

            if (!db.UserRole.Any())
            {
                var userRole = new UserRole()
                {
                    Role = adminRole,
                    User = adminUser
                };

                string salt = crypto.CreateSalt();
                string hash = crypto.CreateKey(salt, "P@ssword");

                var userProvider = new UserProviderLocal
                {
                    CreatedOn    = DateTime.Now,
                    ProviderId   = ProviderTypes.Local,
                    PasswordSalt = salt,
                    PasswordHash = hash,
                    User         = adminUser,
                };

                db.UserRole.Add(userRole);
                db.UserProvider.Add(userProvider);

                db.SaveChanges();
            }

            return(adminUser);
        }
Beispiel #3
0
        private static User EnsureAdmin(DbContextBase db, ICryptoService crypto)
        {
            User admin = db.User.SingleOrDefault(o => o.Username == AdminEmail);

            if (admin == null)
            {
                admin = new User()
                {
                    CultureName = "en",
                    DisplayName = "Webmaster",
                    Enabled     = true,
                    TimeZoneId  = Globalization.DefaultTimeZoneId,
                    Username    = AdminEmail
                };

                db.User.Add(admin);
                db.SaveChanges();
            }

            Role role = db.Role.FirstOrDefault(o => o.RoleId == RoleTypes.Admin);

            if (role == null)
            {
                string name = RoleTypes.System.FirstOrDefault(o => o.Key == RoleTypes.Admin).Value;

                role = new Role()
                {
                    CreatedByUser = admin,
                    Enabled       = true,
                    Name          = name,
                    RoleId        = RoleTypes.Admin
                };

                db.Role.Add(role);
                db.SaveChanges();
            }

            if (!db.UserRole.Any())
            {
                var userRole = new UserRole()
                {
                    Role = role,
                    User = admin
                };

                string salt = crypto.CreateSalt();
                string hash = crypto.CreateKey(salt, "P@ssw0rd");

                var userProvider = new UserProviderLocal
                {
                    ProviderId   = ProviderTypes.Local,
                    PasswordSalt = salt,
                    PasswordHash = hash,
                    User         = admin,
                };

                db.UserRole.Add(userRole);
                db.UserProvider.Add(userProvider);

                db.SaveChanges();
            }

            return(admin);
        }