I have an issue with Identity and I am not very familiar with it. Not too long ago I started a new project that was originally not meant to have any authentication attached to it. However, as the project grew we found that we should implement it. Since it wasn't originally set up that way I created a form for a login.
I found the answer to this question and implemented it:
How to implement custom authentication in ASP.NET MVC 5
However it is not working and I do not know why.
Here's my code:
Nothing much to see here it's just a plain form.
@{
ViewBag.Title = "Login";
}
<div class="container">
<h2>Login</h2>
<br />
@using (Html.BeginForm("Login", "Main"))
{
<div class="row">
<div class="form-group col-xs-6">
@Html.Label("Username", htmlAttributes: new { @class = "control-label col-sm-3" })
<div class="col-sm-8">
@Html.TextBox("username", null, new { @class = "form-control" })
@*Html.ValidationMessageFor(model => model.EnrollmentOption, "", new { @class = "text-danger" })*@
</div>
</div>
<div class="form-group col-xs-6">
</div>
</div>
<div class="row">
<div class="form-group col-xs-6">
@Html.Label("Password", htmlAttributes: new { @class = "control-label col-sm-3" })
<div class="col-sm-8">
@Html.Password("password", null, new { @class = "form-control" })
@*Html.ValidationMessageFor(model => model.EffectiveDate, "", new { @class = "text-danger" })*@
</div>
</div>
<div class="form-group col-xs-6">
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-offset-6 col-sm-5">
<input type="submit" id="login" value="Sign in" class="btn btn-primary" />
</div>
</div>
</div>
}
The action implemented for it is this one, this matters much more:
[HttpPost]
public ActionResult Login(string username, string password)
{
if (isLoginValid(username, password))
{
var ident = new ClaimsIdentity(
new[] {
// adding following 2 claim just for supporting default antiforgery provider
new Claim(ClaimTypes.NameIdentifier, username),
new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
new Claim(ClaimTypes.Name,username),
// No roles needed for now...
},
DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.GetOwinContext().Authentication.SignIn(
new AuthenticationProperties { IsPersistent = false }, ident);
return RedirectToAction("QuoteSearch"); // auth succeed
}
else
{
// invalid username or password
ModelState.AddModelError("", "Invalid username or password");
return View();
}
}
And there's the isLoginValid function (For now it's set to use a hardcoded login)
[NonAction]
private bool isLoginValid(string user, string password)
{
return (user.ToLower() == "someUser" && password.ToLower() == "myPassword");
}
I do not know very much of claims or how Identity works underneath. I did make sure of adding all the necessary references though. When using the authorize attribute on my actions after the login redirects I get an unauthorized request from IIS. Is there something wrong with my code?
What should I change or fix to be able to use the Authorization part of Identity?
Thanks,