Esempio n. 1
0
        public async Task <IActionResult> Create([FromBody] RegistrationLoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Unauthorized());
            }

            var userVerified = await _acctSvc.VerifyUserAsync(model);

            if (userVerified != null)
            {
                var token = new JwtTokenBuilder()
                            .AddSecurityKey(JwtSecurityKey.Create(_config.GetSection("JwtSettings:SecurityKey").Value))
                            .AddSubject(model.Email)
                            .AddIssuer(_config.GetSection("AppConfiguration:Issuer").Value)
                            .AddAudience(_config.GetSection("AppConfiguration:Issuer").Value)
                            //.AddClaim("SellerId", userVerified.Id.ToString())
                            .AddExpiry(10)
                            .Build();

                TokenModel tokenModel = new TokenModel
                {
                    AccessToken = token.Value,
                    SellerId    = userVerified.Id,
                    Email       = model.Email
                };

                return(Ok(tokenModel));
            }

            return(BadRequest());
        }
        public async Task <ApplicationUser> VerifyUserAsync(RegistrationLoginModel model)
        {
            var user = new ApplicationUser {
                Email = model.Email
            };

            var userInfo = await _repo.GetUserInfoAsync(u => u.Email == model.Email);

            var salt         = Convert.FromBase64String(userInfo.Salt);
            var hashPassword = Convert.FromBase64String(userInfo.Password);

            var isVerified = PasswordHashHelper.VerifyPassword(model.Password, salt, hashPassword);

            return(isVerified ? userInfo : null);
        }
Esempio n. 3
0
        public async Task <IActionResult> Create([FromBody] RegistrationLoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.Values.SelectMany(v => v.Errors).Select(modelError => modelError.ErrorMessage).ToList()));
            }

            var user = new ApplicationUser {
                Email = model.Email, Password = model.Password
            };
            var result = await _acctSvc.RegisterUserAsync(user);

            if (!result)
            {
                return(BadRequest("Error registering new user."));
            }

            return(Ok());
        }