为什么User.Identity.IsAuthenticated总是假的? [英] Why User.Identity.IsAuthenticated always false?

查看:110
本文介绍了为什么User.Identity.IsAuthenticated总是假的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想在下面获得用户名

Hi everyone,
I want to get username in below

<pre lang="c#">

 public ActionResult Login(LoginModel model, string returnUrl)
        {
 if (User.Identity.IsAuthenticated)//this is get always false
            {
string user = User.Identity.Name;//here i need username
            }
  
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password,     persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }




User.Identity.IsAuthenticated always return false so userid comes always -1, I am not even able to short out my mistake, what mistake am doing and how can i overcome with this, please help me.. 

推荐答案

User.Identity.IsAuthenticated在调用FormsAuthentication.SetAuthCookie()之后,在下一个请求之前不会设置为true。



请参阅http://msdn.microsoft.com/en-us/ library / twk5762b.aspx



SetAuthCookie方法将一个表单身份验证票证添加到cookie集合,或者如果CookiesSupported为false,则添加到URL。表单身份验证票证将表单身份验证信息提供给浏览器发出的下一个请求。
User.Identity.IsAuthenticated won't be set to true until the next request after calling FormsAuthentication.SetAuthCookie().

See http://msdn.microsoft.com/en-us/library/twk5762b.aspx

The SetAuthCookie method adds a forms-authentication ticket to either the cookies collection, or to the URL if CookiesSupported is false. The forms-authentication ticket supplies forms-authentication information to the next request made by the browser.


这篇关于为什么User.Identity.IsAuthenticated总是假的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆