2016-11-27 6 views
0

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> 

Antwort

0

User.Identity.IsAuthenticated überprüft den Authentifizierungscookie vom Client, um festzustellen, ob der Benutzer angemeldet ist oder nicht. Da das Authentifizierungscookie nicht vorhanden ist, wenn Sie an Ihre Anmeldemethode senden, gibt es immer false zurück. Warum führen Sie die Überprüfung direkt nach dem Anmelden des Benutzers aus? Die Überprüfung sollte tatsächlich mit der GET-Methode login durchgeführt werden.

public ActionResult Login(string returnUrl) 
    { 
     if (User.Identity.IsAuthenticated) 
     { 
      //already logged in - no need to allow login again!! 
      return RedirectToAction("Index", "Home"); 
     } 
     ViewBag.ReturnUrl = returnUrl; 
     return View(); 
    } 

    [AllowAnonymous] 
    [HttpPost] 
    public ActionResult Login(UserProfile register) 
    { 
     //check your model state! 
     if(!ModelState.IsValid) return View(); 

     //this method returns some result letting you know if the user 
     //logged in successfully or not. You need to check that. 

     //Additionally, this method sets the Auth cookie so you can 
     //do you IsAuthenticated call anywhere else in the system 
     var loginResult = WebSecurity.Login(register.UserName, register.password, true); 

     //login failed, display the login view again or go whereever you need to go 
     if(!loginResult) return View(); 

     //Good to go, user is authenticated - redirect to where need to go 
     return RedirectToAction("Index", "Home"); 
    } 

Here is the MSDN für die WebSecurity.Login Methode

+0

es ist immer falsch: HasUserID = false, IsAuthenticated = false:/ –

+0

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

+0

Ich versuche formsauthentication.setauthcookie zu verwenden und es ist in Ordnung, es ist das Problem von Cookies in WebSecurity.Login. –

Verwandte Themen