简体   繁体   中英

Forms Authentication Cookie is not setting?

I am trying to use FormsAuthentication.RedirectFromLoginPage(username,true,cookiepath); On using FormsAuthentication.RedirectFromLoginPage it's redirecting to the DefaultUrl provided in the web.config.

Authentication section in web.config:

<authentication mode="Forms">
<forms name=".ASPXADMINAUTH" 
               loginUrl="/Default.aspx" 
               defaultUrl="homepage.aspx"
               protection="All"
              timeout="30" path="/admin" slidingExpiration="true" enableCrossAppRedirects="false" cookieless="UseCookies" domain="localhost" ticketCompatibilityMode="Framework20" ></forms>
</authentication>

In httpModules Section:

<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />

The page is redirecting from the Loginpage to the "homepage.aspx" but it didn't set the Authentication cookie.

In my Response header , Set-Cookie contains the Authentication cookie, but it is not set in the homepage.aspx page.

So the LoginStaus and LoginName control is not working.

The problems in your code are path="/admin" domain="localhost"

According to your code

After user logins, a cookie is set under /admin. As the result, every pages under /admin folder knows that the user is authenticated such as ~/admin/default.aspx.

However ~/homepage.aspx does not know about user, because ~/homepage.aspx cannot read cookie written under /admin.

var path = FormsAuthentication.FormsCookiePath;
FormsAuthentication.RedirectFromLoginPage("win", false, path);

How to fix it?

You want to start slowly using simple one. Then tweak depending on what you need.

<forms loginUrl="~/Default.aspx" timeout="2880" defaultUrl="~/homepage.aspx" />

FYI: Please do not add properties which are default such as slidingExpiration="true" , enableCrossAppRedirects="false" and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM