I’m creating an ASP.NET Web API and MVC web app with SQL Server. It is working well, authorization is successful, but while I’m testing the login screen, if I enter something wrong like a wrong password, it is not returning a warning.
Here is my AccountController
‘s login code
[HttpPost]
public async Task<ActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("https://localhost:7180/api/auth/login", content);
if (response.IsSuccessStatusCode)
{
var tokenResponse = await response.Content.ReadAsStringAsync();
var token = JsonConvert.DeserializeObject<TokenResponse>(tokenResponse);
if (HttpContext.Session != null)
{
HttpContext.Session.SetString("JWToken", token.Token);
}
else
{
ModelState.AddModelError("", "Session has not been configured.");
return View(model);
}
TempData["Token"] = token.Token;
}
else
{
ModelState.AddModelError("", "Kullanıcı adı veya şifre
hatalı.");
ViewData["ErrorMessage"] = "Kullanıcı adı veya şifre
hatalı.";
}
}
return View(model);
}
And my login.cshtml
view:
@page
@model WebApplication2.Models.LoginViewModel
@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
@using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
<div class="form-group">
@Html.LabelFor(m => m.ADI)
@Html.TextBoxFor(m => m.ADI, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.SOYADI)
@Html.TextBoxFor(m => m.SOYADI, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.KULLANICI_ADI)
@Html.TextBoxFor(m => m.KULLANICI_ADI, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.SIFRE)
@Html.PasswordFor(m => m.SIFRE, new { @class = "form-control" })
</div>
<button type="submit" class="btn btn-primary">Login</button>
@if (ViewData != null && ViewData.ModelState.ContainsKey(""))
{
<div class="alert alert-danger mt-3" role="alert">
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
</div>
}
}
@if (TempData["Token"] != null)
{
<div class="alert alert-success mt-3" role="alert">
Bearer Token:
<button class="btn btn-info" id="toggleToken">Toggle Token</button>
<div id="tokenContent" style="display: none;">
<pre>@TempData["Token"]</pre>
</div>
</div>
<div><p><a href="GetUsers">getallusers</a></p></div>
}
@section Scripts {
<script>
$(document).ready(function () {
$('#toggleToken').click(function () {
$('#tokenContent').slideToggle();
});
});
</script>
}
I am expecting while entering the wrong name, surname, username or password or everything I want to see a warning about them…
Thanks a lot
New contributor
Göksu Ünsal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2