我有一个网站,现在我正在按照本教程(http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1)使用ASP.NET Identity,Owin等添加身份验证。 ..目前,我创建了自己的自定义表单,而不是使用html帮助器和下面的视图片段,但由于某种原因,我认为我没有正确传递返回网址,因此我收到404错误。有人可以帮我吗?我是.NET MVC的初学者。提前致谢。
@Html.ValidationSummary(true)
@using (Html.BeginForm())
{
@Html.EditorForModel()
<p>
<button type="submit">Log In</button>
</p>
}
模型
public class LogInModel
{
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[HiddenInput]
public string ReturnUrl { get; set; }
}
控制器
[AllowAnonymous]
public class AuthController : Controller
{
// GET: Auth
[HttpGet]
public ActionResult LogIn(string returnUrl)
{
var model = new LogInModel
{
ReturnUrl = returnUrl
};
return View(model);
}
[HttpPost]
public ActionResult LogIn(LogInModel model)
{
if (!ModelState.IsValid)
{
return View();
}
// Don't do this in production!
if (model.Email == "[email protected]" && model.Password == "#G16#")
{
var identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, "Admin"),
new Claim(ClaimTypes.Email, "[email protected]"),
new Claim(ClaimTypes.Country, "USA")
},
"ApplicationCookie");
var ctx = Request.GetOwinContext();
var authManager = ctx.Authentication;
authManager.SignIn(identity);
return Redirect(GetRedirectUrl(model.ReturnUrl));
}
// user authN failed
ModelState.AddModelError("", "Invalid email or password");
return View();
}
private string GetRedirectUrl(string returnUrl)
{
if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl))
{
return Url.Action("index", "home");
}
return returnUrl;
}
public ActionResult LogOut()
{
var ctx = Request.GetOwinContext();
var authManager = ctx.Authentication;
authManager.SignOut("ApplicationCookie");
return RedirectToAction("index", "home");
}
}
看法
<div class="col-md-4" style="margin-top:200px;">
@using (Html.BeginForm("LogInModel", "LogIn", FormMethod.Post, new { @class="form-horizontal" }))
{
<div class="form-group">
<label class="label label-default">Email Address</label>
<div class="form-control">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control"})
</div>
</div>
<div class="form-group">
<label class="label label-default">Password</label>
<div class="form-control">
@Html.TextBoxFor(m => m.Password, new { @class = "form-control" })
</div>
</div>
@Html.HiddenFor(m => m.ReturnUrl)
<button class="btn btn-default" type="submit">Log In</button>
}
</div>
您使用不Html.BeginForm
正确。
代替:
Html.BeginForm("LogInModel", "LogIn", FormMethod.Post, new {})
它应该是:
Html.BeginForm("LogIn", "Auth", FormMethod.Post, new {})
正确用法:
Html.BeginForm("actionName", "controllerName", FormMethod.Post, new {})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句