public async Task <IActionResult> LoginAsync([FromBody] Loginview model)
        {
            if (ModelState.IsValid)
            {
                var result = await _userService.LoginUserAsync(model);

                if (result.IsSuccess)
                {
                    return(Ok(result));
                }
                return(BadRequest(result));
            }
            return(BadRequest("Errors occured your forms is not valid"));
        }
        public async Task <IActionResult> Login(Loginview model)
        {
            if (ModelState.IsValid)                                                                                       //means when we pass variable from model/form data
            {
                var result = await signManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false); //registration /insert data

                if (result.Succeeded)                                                                                     //if create user will be successful then sign in that account
                {
                    //is persistent for data save as session
                    return(RedirectToAction("index", "Home")); //means that it will direct to home controller and go to index action/index page
                }
                //else


                ModelState.AddModelError("", "Invalid Email and Password");
            }
            return(View(model));
        }
Example #3
0
        public async Task <UserManagerResponse> LoginUserAsync(Loginview model)
        {
            if (model == null)
            {
                throw new NullReferenceException("invalid forms");
            }
            var result = await _signManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false); //registration /insert data

            if (result.Succeeded)                                                                                      //if create user will be successful then sign in that account
            {
                //
                var claims = new[]
                {
                    new Claim("Email", model.Email),
                    new Claim(ClaimTypes.NameIdentifier, model.Password),
                    new Claim(ClaimTypes.Name, model.Email),
                };
                //ValidAudience = Configuration["AuthSettings:Audience"],
                //ValidIssuer = Configuration["AuthSettings:Issuer"],
                var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["AuthSettings:keys"]));
                var token = new JwtSecurityToken(
                    issuer: _configuration["AuthSettings:Audience"],
                    audience: _configuration["AuthSettings:Issuer"],
                    claims: claims,
                    expires: DateTime.Now.AddDays(30),
                    signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256));

                string tokenAsstring = new JwtSecurityTokenHandler().WriteToken(token);
                return(new UserManagerResponse
                {
                    Message = tokenAsstring,
                    IsSuccess = true,
                    Expired = token.ValidTo
                });

                //
            }
            return(new UserManagerResponse
            {
                Message = "User not exist please create account",
                IsSuccess = false,
            });
        }