Hi. I'm implementing asp.net core project and I want to connect my web app to the active directory via Ldap. For doing that I implemented some code like the following, However there is an error "An unhandled exception occurred while processing the request.
InvalidOperationException: The view 'Login' was not found. The following locations were searched:
/Views/Account/Login.cshtml
/Views/Shared/Login.cshtml".
While other of my views related to other controllers work correctly.
I appreciate if any one can suggest me a solution to fix the error
What I have tried:
namespace MyDashboard.Controllers
{
public class AccountController : Controller
{
private readonly IAuthenticationService _authenticationService;
public AccountController(IAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginModel model)
{
var result = _authenticationService.ValidateUser("tehran.iri",model.UserName, model.Password);
if (result)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, model.UserName),
new Claim(ClaimTypes.Role, "Administrator"),
};
var claimsIdentity = new ClaimsIdentity(
claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
};
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
}
return Ok();
}
public IActionResult Index()
{
var user = HttpContext.User.Identity.Name;
return View();
}
}
}
And here is my Login.cshtml
@model MyDashboard.Models.LoginModel
@{
ViewData["Title"] = "Login";
}
<div class="row">
<div class="hidden-xs-down col"></div>
<div class="col">
<div class="card" style="margin: 20px 0;">
<div class="card-header">
Login
</div>
<div class="card-body">
<h4 class="card-title"></h4>
@{
var errorMessage = this.TempData["ErrorMessage"]?.ToString();
if (!string.IsNullOrEmpty(errorMessage))
{
<div class="alert alert-danger">
Oops! @errorMessage
</div>
}
}
<form asp-route-returnurl="@this.ViewData["ReturnUrl"]" method="post">
<span class="card-text">Use an LDAP account to sign in.</span>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<div class="input-group">
<span class="input-group-prepend">
<span class="input-group-text">
class="fa fa-user-o">
</span>
</span>
<input asp-for="UserName" placeholder="Username *" class="form-control" />
</div>
<span asp-validation-for="UserName" class="text-danger"></span>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-prepend">
<span class="input-group-text">
^__i class="fa fa-lock">
</span>
</span>
<input asp-for="Password" placeholder="Password *" class="form-control" />
</div>
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<div class="checkbox">
<label asp-for="RememberMe">
<input asp-for="RememberMe" />
@Html.DisplayNameFor(m => m.RememberMe)
</label>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Signin</button>
</div>
</form>
</div>
</div>
</div>
<div class="hidden-xs-down col"></div>
</div>
@section Scripts {
@await Html.PartialAsync("_ValidationScriptsPartial")
}
public class LoginModel
{
public string UserName { get; set; }
public string Password { get; set; }
}