asp.net mvc身份表不';t post to allowanonymous action
我正在学习身份,我有一个似乎没有人有过的问题。如果我自己进行操作,我的应用程序登录就可以正常工作,但如果我尝试像通常一样使用表单和帖子进行登录。然后我的表单数据就不能进入动作了。我在该操作中有一个断点,代码永远不会命中。我没有像AntiForgeryToken这样的东西,因为我只想达到断点 登录操作asp.net mvc身份表不';t post to allowanonymous action,asp.net,asp.net-mvc,post,asp.net-identity,Asp.net,Asp.net Mvc,Post,Asp.net Identity,我正在学习身份,我有一个似乎没有人有过的问题。如果我自己进行操作,我的应用程序登录就可以正常工作,但如果我尝试像通常一样使用表单和帖子进行登录。然后我的表单数据就不能进入动作了。我在该操作中有一个断点,代码永远不会命中。我没有像AntiForgeryToken这样的东西,因为我只想达到断点 登录操作 [HttpPost] [AllowAnonymous] public virtual ActionResult Login(UserViewModel user, string re
[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"/>
}
@使用(Html.BeginForm(“Login”,“Admin”,new{returnUrl=ViewBag.returnUrl},FormMethod.Post))
{
@Html.ValidationSummary(true)
@LabelFor(m=>m.Name)
@TextBoxFor(m=>m.Name,新的{@class=“formcontrol”,placeholder=“Jméno”})
@Html.ValidationMessageFor(m=>m.Name)
@LabelFor(m=>m.Password)
@Html.PasswordFor(m=>m.Password,新的{@class=“form control”,placeholder=“Heslo”})
@Html.ValidationMessageFor(m=>m.Password)
@LabelFor(m=>m.ConfirmPassword)
@PasswordFor(m=>m.ConfirmPassword,新的{@class=“form control”,placeholder=“Potvrdit heslo”})
@Html.ValidationMessageFor(m=>m.ConfirmPassword)
}
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}")
});
}
public void ConfigureAuth(IAppBuilder应用程序)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext(ApplicationUserManager.Create);
app.CreatePerOwinContext(ApplicationSignInManager.Create);
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串($“/{MVC.Admin.Name}/{MVC.Admin.ActionNames.Login}”)
});
}
问题在于返回登录视图的操作。我没有将模型作为对象作为参数传递给我的视图。发布表单时是否有任何错误?您显示的代码工作正常。如果它不为您工作,是因为您没有向我们展示代码。你所说的“工作很好”是什么意思?如果我自己动手的话。我检查了it浏览器开发工具,并成功发布了它。我将尝试创建新项目,并将所有内容复制粘贴到新项目。