2016-04-03 84 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

相关问题