Pourquoi User.Identity.IsAuthenticated est toujours faux ?

la programmation


Salut tout le monde,
Je veux obtenir le nom d’utilisateur ci-dessous

<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.. 

Solution 1

User.Identity.IsAuthenticated ne sera pas défini sur true avant la prochaine requête après l’appel de FormsAuthentication.SetAuthCookie().

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

La méthode SetAuthCookie ajoute un ticket d’authentification par formulaire soit à la collection de cookies, soit à l’URL si CookiesSupported est faux. Le ticket d’authentification par formulaire fournit des informations d’authentification par formulaire à la prochaine requête effectuée par le navigateur.

Solution 2

Salut à tous, je testais l’authentification de compte Microsoft avec OWIN et notez que vous devez mettre l’annotation Authorize au moins dans un contrôleur, j’espère que cela vous aidera

コメント

タイトルとURLをコピーしました