2016-04-03 51 views
0

我已經設置與窗體身份驗證一個簡單的登錄,在一個ASP.NET MVC應用程序在我的本地正常工作我應該用什麼來代替身份驗證的用戶在我的.cshtml與窗體身份驗證

我可以訪問登錄表單在實時應用中發佈它。

但是,當我將網站發佈到Web上的實時應用程序時,表單身份驗證不起作用。我究竟做錯了什麼?

這裏是我的代碼:

<system.web> 
     <authentication mode="Forms"> 
      <forms loginUrl="~/Home/Login" timeout="30" /> 
     </authentication> 
</system.web> 

控制器:

public ActionResult LoginUser(string username, string password) 
    { 
     if (ConfigurationManager.AppSettings["UserName"] == username && ConfigurationManager.AppSettings["Password"] == password) 
     { 
      FormsAuthentication.SetAuthCookie(username, false); 

      return RedirectToAction("Index"); 
     } 
     return RedirectToAction("Index"); 
    } 

的Javascript:

$("body").on("click", "#loginbutton", function (e) { 

$("#loginbutton").attr("href", "/loginuser?username=" + $('[user]').attr('user') + "&password=" + $('[pass]').attr('pass')); 

}); 

.cshtml:

@if (User.Identity.IsAuthenticated) 
     { 
      <div class="row"> 
       <div class="col-xs-12"> 
        <a href="#uploadimage" class="md light btn-popup upload-cloud"><i class="fa fa-cloud-upload" id=""></i></a> 
       </div> 
      </div> 
     } 

爲什麼這不適用於實時應用程序?

UPDATE:

我現在已經foud,它並沒有在本地主機上的工作也。問題是@if (User.Identity.IsAuthenticated) 我應該在我的.cshtml文件中使用經過身份驗證的用戶身份?

+0

這是什麼意思,它不工作。你觀察到了什麼差異?讓其他人通過擴大你的答案和必要的細節來幫助你。 –

+0

@WiktorZychla我已更新我的問題 – user3228992

回答

0

也許一個愚蠢的答案,但你確實有你的web.release.config中的鍵,而不是隻有web.debug.config?

如果您在嘗試登錄時遇到500或400錯誤,您是否可以在瀏覽器中看到devtools?也可能是路由問題。

+0

我現在已經發現它也不能在本地主機上工作。問題是'@if(User.Identity.IsAuthenticated)' 我應該在我的.cshtml文件中使用身份驗證的用戶? – user3228992

+0

這是因爲您使用javascript登錄。 FormsAuthentication.SetAuthCookie會在響應中發送一個cookie,但瀏覽器在您重新加載該站點之前不會設置cookie。你在這裏有一些相關的:http://stackoverflow.com/questions/17642630/user-identity-isauthenticated-is-false-after-successful-login – Hypnobrew

+0

我重新加載網站? 'return RedirectToAction(「Index」);' – user3228992