Ejemplo n.º 1
0
        /// <summary>
        /// Аутентификация пользователя
        /// </summary>
        /// <param name="username">Логин</param>
        /// <param name="password">Пароль</param>
        /// <returns></returns>
        public async Task <UserViewModel> Authenticate(string userName, string password)
        {
            var helper     = new JwtHelper();
            var userHelper = new DomainUserHelper();
            var domainUser = userHelper.User(userName, password, _appSettings.Domain).Result;

            if (domainUser == null)
            {
                return(null);
            }

            var user = await _users.GetUser(domainUser.Guid.Value);

            var userForToken = userHelper.UserForToken(domainUser);
            var token        = await helper.CreateJwtAsync(userForToken, _appSettings.Issuer, _appSettings.Audience, _appSettings.Secret, _appSettings.DaysValid);


            if (user == null)
            {
                await _users.AddUser(domainUser, token);
            }
            else
            {
                await _users.PutToken(domainUser.Guid.Value, token);
            }
            await _users.SaveChanges();

            UserViewModel userView = userHelper.UserToSend(userForToken, token);

            return(userView);
        }