c# - why Request.IsAuthenticated return true but session is null -
in asp.net mvc project. use area named office.
area |--office |--controllers |--accountcontroller |-- methods: index (authorized) controllers |--homecontroller |-- methods: index (not required authorized)
share folder in project(not in area) contains: _loginpartial.chtml, _layout.chtml (use _loginpartial.chtml)
this code in _loginpartial.chtml:
@if (request.isauthenticated) { <div class="dropdown"> <span data-toggle="dropdown"> <span><b>hello</b></span> <span><b>@user.identity.name</b></span> </span> <ul class="dropdown-menu" role="menu" aria-labelledby="dlabel"> <li role="presentation"> <a role="menuitem" tabindex="-1" href="/office/account/password">change password</a> </li> </ul> </div> <span>.</span> using (html.beginform("logoff", "home", formmethod.post, new { id = "logoffform" })) { @html.antiforgerytoken() <a href="javascript:document.getelementbyid('logoffform').submit()">exit</a> } } else { <text>log in</text> }
i use method session of user , if session null, redirect login page. code in basecontroller
protected account account { { bool flag = true; if (session["user"] == null) flag = this.relogin(); if (flag == false) return null; account account = (account)session["user"]; return account; } } protected bool relogin() { httpcookie cookieusername = request.cookies["username"]; if (cookieusername == null) return false; httpcookie cookiepassword = request.cookies["password"]; if (cookiepassword == null) return false; string username = cookieusername.value; string password = cookiepassword.value; account account = this.loginwithencrypt(username, password); this.setsession(account); return account != null; } private void setsession(account account) { if (account == null || account.issuspend == true) return; session["user"] = account; this.setcookie("username", account.code); this.setcookie("password", account.password); } protected account login(string username, string password) { accountbl accountbl = new accountbl(); account account = accountbl.loadbycodeandpassword(username, password); this.setsession(account); return account; } protected void logout() { formsauthentication.signout(); session["user"] = null; this.removecookie("username"); this.removecookie("password"); }
this code login in homecontroller (inherited basecontroller) in office area:
[httppost] public actionresult login(string username, string password, string returnurl) { account account = this.login(username, password); if (account == null) { viewbag.error = "1"; return view(); } else { formsauthentication.setauthcookie(account.code, true); if (string.isnullorwhitespace(returnurl)) return redirecttoaction("index"); else return redirect(returnurl); } }
i go localhost/home/index, display username. after that, go localhost/office/account/index, said i'm not log in redirect me login page.
my question: if request.isauthenticated true, session null. has wrong , cannot figure out.
i try set formsauthentication.setauthcookie(account.code, true) , when log out, remove cookie , set formsauthentication.signout. doesn't work.
my target: if show username, user have session on site.
if doesn't clear you, please comment below , edit it.
thanks
Comments
Post a Comment