我如何使用 FormsAuthentication:
app1.example.com:
- 包含登录页面(login.aspx)
- 如果未通过身份验证,则重定向
网络配置:
<authentication mode="Forms"> <forms name="example" loginUrl="login.aspx" domain=".example.com" protection="All" path="/" timeout="30" cookieless="UseCookies" enableCrossAppRedirects="true" /> </authentication>
app2.example.com:
- 如果未通过身份验证,只需重定向 ( http://app1.example.com/login.aspx )
网络配置:
<authentication mode="Forms"> <forms name="example" loginUrl="http://app1.example.com/login.aspx" domain=".example.com" protection="All" path="/" timeout="30" cookieless="UseCookies" enableCrossAppRedirects="true" /> </authentication>
对于登录,我执行以下操作:
FormsAuthentication.SetAuthCookie(
userId.ToString(), false);
FormsAuthenticationTicket ticket1 =
new FormsAuthenticationTicket(
1,
userId.ToString(),
DateTime.Now,
DateTime.Now.AddMinutes(20),
false,
sessionid // sessionid
);
HttpCookie cookie1 = new HttpCookie(
FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(ticket1));
HttpContext.Current.Response.Cookies.Add(cookie1);
string returnUrl = HttpContext.Current.Request["redirect"] == null ? "app1.example.com" : HttpContext.Current.Request["redirect"];
HttpContext.Current.Response.Redirect(returnUrl);
在每个应用程序上,我都必须从 AuthCookie 中读出我的自定义“sessionid”。在 App2 上,我得到了正确的 ID,而在包含登录页面的 App1 上,我没有(它是空的)。
有谁知道它是怎么来的?
这是我读取用户数据的代码:
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
所有应用程序都在 Asp.Net 4.5.2 上运行
提前致谢!