Click here to Skip to main content
15,891,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
            {
                //AllowRefresh = <bool>,
                // Refreshing the authentication session should be allowed.

                //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
                // The time at which the authentication ticket expires. A 
                // value set here overrides the ExpireTimeSpan option of 
                // CookieAuthenticationOptions set with AddCookie.

                //IsPersistent = true,
                // Whether the authentication session is persisted across 
                // multiple requests. When used with cookies, controls
                // whether the cookie's lifetime is absolute (matching the
                // lifetime of the authentication ticket) or session-based.

                //IssuedUtc = <DateTimeOffset>,
                // The time at which the authentication ticket was issued.

                //RedirectUri = <string>
                // The full path or absolute URI to be used as an http 
                // redirect response value.
            };
            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; }
    }
Posted
Updated 29-May-20 1:23am
Comments
ZurdoDev 29-May-20 7:18am    
What's the path to your login.cshtml?

1 solution

If your view isn't at the locations listed you'll need to specify exactly where it is;

C#
public IActionResult Login()
{
    return View("~/path/to/your/login.cshtml");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900