Пример #1
0
        public async Task <LoginResult> RegisterAsync(RegisterModel registerModel)
        {
            var exists = authDataProvider.Users.FirstOrDefault(x => x.Email == registerModel.Email);

            if (exists != null)
            {
                return(null);
            }

            var newUser = new User
            {
                Email        = registerModel.Email,
                Nickname     = registerModel.Nickname,
                PasswordHash = CryptService.CreateHash(registerModel.Password),
                Role         = authDataProvider.Roles.FirstOrDefault(x => x.Name == "User")
            };

            authDataProvider.Languages.Load();

            authDataProvider.Add(newUser);
            await authDataProvider.SaveChangesAsync();

            var res = await GenerateTokenAndResult(newUser);

            return(res);
        }
Пример #2
0
        public async Task <LoginResult> RestorePassword(PasswordRestore passwordRestore, string token)
        {
            var tokenInfo = tokenService.GetPrincipalFromExpiredToken(token);
            var exists    = authDataProvider.Users.FirstOrDefault(x => x.Id == passwordRestore.UserId);

            if (exists == null || exists.Email != tokenInfo.Identity.Name || exists.PasswordHash != CryptService.CreateHash(passwordRestore.PrevPassword))
            {
                return(null);
            }

            exists.PasswordHash = CryptService.CreateHash(passwordRestore.Password);

            var res = await GenerateTokenAndResult(exists);

            await authDataProvider.SaveChangesAsync();

            return(res);
        }
Пример #3
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity <Role>().Property(x => x.Name).HasMaxLength(100);
            modelBuilder.Entity <User>().Property(x => x.Email).HasMaxLength(400);

            modelBuilder.Entity <Role>().HasIndex(x => x.Name);
            modelBuilder.Entity <User>().HasIndex(x => x.Email);

            modelBuilder.Entity <Language>().HasIndex(x => x.Name);

            modelBuilder.Entity <Language>().HasData(new[] {
                new Language {
                    Id = 1, Name = "English", Code = "en", UICode = "en-US"
                },
                new Language {
                    Id = 2, Name = "Русский", Code = "ru", UICode = "ru-RU"
                }
            });

            modelBuilder.Entity <User>().Property("LanguageId").HasDefaultValue(1);
            modelBuilder.Entity <User>().Property("RoleId").HasDefaultValue(1);

            modelBuilder.Entity <Role>().HasData(new[] {
                new Role {
                    Id = 1, Name = "User"
                },
                new Role {
                    Id = 2, Name = "Admin"
                },
                new Role {
                    Id = 3, Name = "Service"
                },
            });

            modelBuilder.Entity <User>().HasData(new[] {
                new User {
                    Email = "*****@*****.**", LanguageId = 1, Nickname = "admin", Id = 1, PasswordHash = CryptService.CreateHash("admin"), RoleId = 2
                },
                new User {
                    Email = "*****@*****.**", LanguageId = 1, Nickname = "service", Id = 2, PasswordHash = CryptService.CreateHash("service"), RoleId = 3
                },
            });
        }
Пример #4
0
        public async Task <LoginResult> LoginAsync(LoginModel loginModel)
        {
            var exists = authDataProvider.Users.FirstOrDefault(x => x.Email == loginModel.Login && x.PasswordHash == CryptService.CreateHash(loginModel.Password));

            if (exists == null)
            {
                return(null);
            }
            var res = await GenerateTokenAndResult(exists);

            await authDataProvider.SaveChangesAsync();

            return(res);
        }