2016-04-05 15 views
0

asp.net-mvc5 proyectのHttpContext.Current.User.Identityにログインに関する情報を保存しようとしています。しかし、すべてが空です。MVCログイン。 User.Identity always empty

すべてのものに、コントローラ付きのビューがあります。あなたは、ログインボタンをクリックしてコントローラに呼び出す :

public ActionResult Button1_Click(string user, string pass) 
{ 
    bool result = _model.ValidateLogin(user, pass, 3, false); 

    DirectResult r = new DirectResult(); 

    // Do some Authentication... 
    if (!result) 
    { 
     r.Success = false; 
     r.ErrorMessage = "Invalid username or password."; 
    } 

    return r; 
} 

方法ValidateLogin、最初のユーザーを確認するには、データベース上で、idのクッキーを作成します。

DateTime now = DateTime.Now; 
System.Web.Security.FormsAuthentication.Initialize(); 
System.Web.Security.FormsAuthenticationTicket ticket = new System.Web.Security.FormsAuthenticationTicket(1, userId.ToString(), now, now.Add(System.Web.Security.FormsAuthentication.Timeout), checkRemember, string.Empty, System.Web.Security.FormsAuthentication.FormsCookiePath); 
string hash = System.Web.Security.FormsAuthentication.Encrypt(ticket); 
System.Web.HttpCookie cookie = new System.Web.HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, hash); 
if (ticket.IsPersistent) 
cookie.Expires = now.AddYears(1); 
System.Web.HttpContext.Current.Response.Cookies.Add(cookie); 

しかし、ときに私いつも空で、「にisAuthenticatedが」偽であるUser.Identifyをチェックしてみてください:

protected override System.Security.Principal.IIdentity GetClientIdentity() 
{ 
    IIdentity identity = System.Web.HttpContext.Current.User.Identity; 
    if (identity.IsAuthenticated) 
     return identity; 
    else 
     throw new AuthorizationDeniedException("Not logged in",false); 

} 

なぜ?何か案が? web.configファイルを追加するための

編集:Web構成で

、私が持っている:

<authentication mode="Forms"> 
     <forms loginUrl="~/Login/Index" protection="All" timeout="120" name=".ASPXAUTH" path="/" requireSSL="false" slidingExpiration="true" defaultUrl="default.html" cookieless="UseDeviceProfile" enableCrossAppRedirects="false"/> 
</authentication> 
<authorization> 
     <deny users="?"/> 
</authorization> 
<location path="Login"> 
     <system.web> 
      <authorization> 
       <allow users="*"/> 
      </authorization> 
     </system.web> 
</location> 

答えて