2016-11-27 6 views
0

Webセキュリティ認証に問題があります。ログインできません。認証retrurn常にfalse 私は常にログインページに私を送信します。私はそれをデバッグし、私はこの問題は、常にfalseを返しhttpContext.Request.IsAuthenticated見つけ、任意のヘルプ... コントローラー:Webセキュリティは常に「偽」を返します。

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> 

答えて

0

User.Identity.IsAuthenticatedは、クライアントからの認証Cookieを調べて、ユーザーがログインしているかどうかを判断します。ログインメソッドにPOSTするときに認証Cookieが存在しないため、常にfalseが返されます。さらに、なぜユーザーをログインした直後にチェックを実行するのですか?チェックは実際にはログインGETメソッドで実行する必要があります。

+0

WebSecurity.Login方法について

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それは常にfalseです:HasUserID = falseの場合、にisAuthenticated =偽:/ –

+0

あなたはPOSTログインメソッドの内部でチェックしている場合、それはなります。クッキーなどのために別のアクションにリダイレクトする必要があります。 al。自由に使えるようにする。 1)ログインし、2)上記のコードを使用してログインページに戻ります。再度、あなたの 'WebSecurity.Login'メソッドへの呼び出しを成功させなければなりません**そして**あなたは、そのCookieが適用されるための別のリクエストにリダイレクトする必要があります。 – Tommy

+0

私はformsauthentication.setauthcookieを使用しようとしていますが、それはWebSecurity.Loginのクッキーの問題です。 –

関連する問題