public async Task <ActionResult> Login(UserCreateBindingModel model)
        {
            var currenUser = await this.context
                             .Users
                             .SingleOrDefaultAsync(user => user.Username == model.Username && user.Password == model.Password);

            if (currenUser == null)
            {
                return(this.BadRequest("Username or password is invalid."));
            }

            var tokenHandler = new JwtSecurityTokenHandler();
            var key          = Encoding.ASCII.GetBytes(this.jwtSettings.Secret);

            var tokenDescriptor = new SecurityTokenDescriptor()
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.NameIdentifier, currenUser.Username)
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(
                    new SymmetricSecurityKey(key),
                    SecurityAlgorithms.HmacSha256Signature)
            };

            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(this.Ok(token));
        }
        public async Task <ActionResult> Register(UserCreateBindingModel model)
        {
            this.context.Users.Add(new User
            {
                Username = model.Username,
                Password = model.Password
            });

            await this.context.SaveChangesAsync();

            return(this.Ok());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create(UserCreateBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(View(model ?? new UserCreateBindingModel()));
            }

            var userCreateServiceModel = this.mapper.Map <UserCreateServiceModel>(model);
            var userData = await this.userServices.CreateUser(userCreateServiceModel);

            var callbackUrl = Url.Page(
                "/Account/ConfirmEmail",
                pageHandler: null,
                values: new { area = "Identity", userId = userData.UserId, code = userData.Code },
                protocol: Request.Scheme);

            var isVerificationEmailSent = await this.userServices.SendVerificationEmail(callbackUrl, userData.Email);

            return(RedirectToAction(nameof(Index)));
        }