Skip to content
Loading
Authentication not working when login submited in .Net Core Mvc  
  •                 class="col-md-4">  
  •                     class="container_form">  
  •                         "~/assets/img/LoginUserIcon.jpg" height="150px" width="295px" />  
  •                         class="text text-center alert alert-primary">  
  •                             Welcome Back !   
  •                             Sign In to continue use   
  •                             Klinik Information System  
  •                           
  •                           
  •                         class="form-group">  
  •                             for="UserName">  
  •                                 User Name  
  •                               
  •                             class="input-group mb-3">  
  •                                 class="input-group-text">class="fa fa-user">  
  •                                 "text" asp-for="User_Name" class="form-control" placeholder="Enter User Name" required />  
  •                                   
  •                               
  •                           
  •                         class="form-group">  
  •                             for="Password">  
  •                                 Password  
  •                               
  •                             class="input-group mb-3">  
  •                                 class="input-group-text">class="fa fa-key">  
  •                                 "password" asp-for="Password" class="form-control" placeholder="Enter Password" required />  
  •                                   
  •                               
  •                           
  •                         class="form-group">  
  •                             class="checkbox">  
  •                                 
  •                                     for="RememberLogin" /> @Html.DisplayNameFor(model => model.RememberLogin)  
  •                                   
  •                               
  •                           
  •                         class="form-group">  
  •                             "right">  
  •                                 "submit" class="btn btn-md btn-success">class="fa fa-lock-open"> Sign In  
  •                               
  •                           
  •                         class="fa fa-arrow-alt-circle-right"> @Html.ActionLink("Register New User""Register")  
  •                       
  •                   
  •               
  •           
  •       
  •     @if (@ViewBag.Message != null)  
  •     {  
  •         "text/javascript" lang="javascript">  
  •             Swal.fire({  
  •                 position: 'top',  
  •                 icon: 'error',  
  •                 title: 'Oops...',  
  •                 text: '@ViewBag.Message'  
  •             });  
  •           
  •     }  
  •     "~/lib/jquery-validation/dist/jquery.validate.js">  
  •     "~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js">  
  •   
  •   
  •  AccountController.cs
    1. [HttpGet]  
    2.         public IActionResult Login(string _ReturnUrl = "/")  
    3.         {  
    4.             LoginModel objLoginModel = new LoginModel();  
    5.             objLoginModel.ReturnUrl = _ReturnUrl;  
    6.             return View(objLoginModel);  
    7.         }  
    8.   
    9.         [HttpPost]  
    10.         [ValidateAntiForgeryToken]  
    11.         public async Task Login(LoginModel login)  
    12.         {  
    13.             if (ModelState.IsValid)  
    14.             {  
    15.                 try  
    16.                 {  
    17.                     using (SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("Db_Klinik")))  
    18.                     {  
    19.                         using (SqlCommand cmd = new SqlCommand(null))  
    20.                         {  
    21.                             con.Open();  
    22.                             cmd.Connection = con;  
    23.                             cmd.CommandType = CommandType.Text;  
    24.                             cmd.CommandText = "Select * From Tbl_User Where User_Name=@User_Name And Password=@Password";  
    25.                             cmd.Parameters.AddWithValue("@User_Name", login.User_Name);  
    26.                             cmd.Parameters.AddWithValue("@Password", login.Password);  
    27.                             cmd.ExecuteNonQuery();  
    28.                             DataTable dt = new DataTable();  
    29.                             dt.Load(cmd.ExecuteReader());  
    30.                             if (dt.Rows.Count > 0)  
    31.                             {  
    32.                                 TempData["pesan"] = "Anda berhasil Login";  
    33.                                 var claims = new List() {  
    34.                                 new Claim(ClaimTypes.NameIdentifier, Convert.ToString(login.User_Name)),  
    35.                                 new Claim("username""admin"),  
    36.                                 new Claim(ClaimTypes.Name, login.User_Name),  
    37.                                 new Claim(ClaimTypes.Role, "admin"),  
    38.                              };  
    39.                                 var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);  
    40.                                 var principal = new ClaimsPrincipal(identity);  
    41.                                 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties()  
    42.                                 {  
    43.                                     IsPersistent = login.RememberLogin  
    44.                                 });  
    45.                                 return LocalRedirect(login.ReturnUrl);  
    46.                             }  
    47.                             else  
    48.                             {  
    49.                                 ViewBag.Message = "Login gagal. Username atau Password anda salah !!!";  
    50.                                 return View(login);  
    51.                             }  
    52.                         }  
    53.                     }  
    54.                 }  
    55.                 catch (Exception)  
    56.                 {  
    57.                     throw;  
    58.                 }  
    59.             }  
    60.             return RedirectToAction("Index""Account");  
    61.         }