Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.. 
Posted

1 solution

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.
 
Share this answer
 
Comments
Gopal Rakhal 12-Feb-15 2:34am    
Yeah, so I have just passed username from model to get required data.
Kyaw Zaw 13-Feb-18 0:02am    
Hi bro. Did you use username or Email to login to the system?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900