0
我有一个与我的websecurity身份验证问题,我无法登录是。身份验证后退始终为假 当我总是登录时,它会将我发送到登录页面。我调试它,我发现了一个问题httpContext.Request.IsAuthenticated总是返回false,任何帮助.. 控制器:网络安全通过身份验证始终返回“false”
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
[AllowAnonymous]
[HttpPost]
public ActionResult Login(UserProfile register)
{
WebSecurity.Login(register.UserName, register.password, true);
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "Home");
}
return RedirectToAction("Index", "Contact");
}
观点:
<h2>@ViewBag.Title.</h2>
<div class="row">
<div class="col-md-8">
<section id="loginForm">
@using (Html.BeginForm("Login", "AccountHopital", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Utilisez un compte local pour vous connecter.</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.password, "", new { @class = "text-danger" })
</div>
</div>
和web.config中:
<system.web>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
<authentication mode="Forms">
<!--<modules>
<remove name="FormsAuthentication" />
</modules>-->
<forms loginUrl="~/AccountHopital/Login" timeout="3600" />
</authentication>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
</system.webServer>
它总是假的:HasUserID =假,Isauthenticated = FALSE:/ –
如果您在POST登录方法的内部检查,这将是。您必须重定向到另一个操作才能使cookie等。人。可用。尝试1)登录和2)使用上面的代码返回到登录页面。再次,您必须成功调用您的WebSecurity.Login方法,并且您必须重定向到另一个要应用Cookie的请求。 – Tommy
我尝试使用formsauthentication.setauthcookie,它没问题,这是WebSecurity.Login中Cookie的问题。 –