c# - HttpContext.User.Identity.IsAuthenticated never true -


login

[httppost] public actionresult login(loginmodel loginmodel) {     if (modelstate.isvalid)     {         using (var _db = new accountcontext())         {             var _useraccount = _db.useraccounts.firstordefault(u => u.username == loginmodel.username && u.password == loginmodel.password);              if (_useraccount == null)             {                 modelstate.addmodelerror("", "account doesn't exist!");             }             else             {                 formsauthentication.setauthcookie(loginmodel.username, false);             }         }     }     return redirecttoaction("index", "home"); } 

redirect or display view

public actionresult index() {     if (httpcontext.user.identity.isauthenticated)     {         return view("index");     }     else     {         return redirecttoaction("loginpage");     } } 

i've stepped through code , can see setauthcookie being called correct username. formsauthentication.setauthcookie(loginmodel.username, false);

what prevent user being authenticated?

what prevent user being authenticated?

one possible reason if forgot enable forms authentication in web.config:

<system.web>     <authentication mode="forms">       <forms loginurl="~/account/login" timeout="2880" />     </authentication>     ... </system.web> 

you might want inspect whether forms authentication cookie being emitted in browser after calling setauthcookie method. default cookie called .aspxauth. if such cookie present , ticket hasn't expired, httpcontext.user.identity.isauthenticated method return true.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -