Ich habe ein Problem mit meiner WebSecurity-Authentifizierung, kann ich nicht einloggen ist. authenticated retrurn immer falsch Wenn ich mich immer anmelde schickt es mich zur Anmeldeseite. Ich gedebuggt es und ich fand ein Problem httpContext.Request.IsAuthenticated immer false zurück, jede Hilfe .. Controller:Web Security ist immer authentifiziert zurück "falsch"
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");
}
Ansicht:
<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>
und 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>
es ist immer falsch: HasUserID = false, IsAuthenticated = false:/ –
Wenn Sie innerhalb des POST Login-Methode prüfen, wird es sein wird. Sie müssen zu einer anderen Aktion umleiten, damit der Cookie et. al. verfügbar sein. Versuchen Sie 1) Login und 2) gehen Sie zurück zum Login mit dem obigen Code. Auch hier müssen Sie Ihre "WebSecurity.Login" -Methode erfolgreich aufrufen ** UND ** Sie müssen zu einer anderen Anfrage umleiten, damit der Cookie angewendet wird. – Tommy
Ich versuche formsauthentication.setauthcookie zu verwenden und es ist in Ordnung, es ist das Problem von Cookies in WebSecurity.Login. –