我正在学习身份,我有一个似乎没有人的问题。我登录到应用程序工作正常,如果我自己进行操作,但如果我试图使它通常登录应该是 - 与表单和帖子。然后我的表单数据只是没有进入操作。我在该动作中有一个断点,代码永远不会命中。我没有像AntiForgeryToken这样的东西,因为我只想打破断点。
登录操作
[HttpPost]
[AllowAnonymous]
public virtual ActionResult Login(UserViewModel user, string returnUrl)
{
if (!ModelState.IsValid)
return RedirectToAction(MVC.Home.ActionNames.Index, MVC.Home.Name);
var result = SignInManager.PasswordSignIn(
user.Name, user.Password, false, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return Redirect(returnUrl);
case SignInStatus.LockedOut:
case SignInStatus.RequiresVerification:
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt");
return RedirectToAction(MVC.Home.ActionNames.Index, MVC.Home.Name);
}
}
登录HTML
@using (Html.BeginForm("Login", "Admin", new { returnUrl = ViewBag.returnUrl }, FormMethod.Post ))
{
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(m => m.Name)
<div class="input-group">
@Html.TextBoxFor(m => m.Name, new {@class = "form-control", placeholder = "Jméno"})
<span class="input-group-addon"><i class="fa fa-user"></i></span>
</div>
@Html.ValidationMessageFor(m => m.Name)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password)
<div class="input-group">
@Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Heslo" })
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
</div>
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="form-group">
@Html.LabelFor(m => m.ConfirmPassword)
<div class="input-group">
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control", placeholder = "Potvrdit heslo" })
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
</div>
@Html.ValidationMessageFor(m => m.ConfirmPassword)
</div>
<input type="submit" value="Přihlásit se" class="btn btn-default"/>
}
Startup.Auth
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString($"/{MVC.Admin.Name}/{MVC.Admin.ActionNames.Login}")
});
}
答案 0 :(得分:1)
问题在于返回Login View的操作。我没有将模型作为对象传递给我作为参数的视图。