使用 asp.net 表单身份验证的单点登录不起作用
Single Sign On using asp.net forms authentication not working
我有两个子域,比如 b1.abc.com 和 s1.abc.com。我正在使用表单身份验证实现单点登录,但它似乎没有按预期工作。我想要的是,如果用户登录 b1.abc.com 然后打开 s1.abc.com 的主页(比如在另一个选项卡中),那么他不应该被重定向回登录页面,而是让他登录并给他看主页。
截至目前,当我登录 b1.abc.com 然后打开 s1.abc.com 时,它不会进行身份验证并重定向到登录页面。
下面是我的代码。
两个应用的登录按钮点击事件:
FormsAuthentication.SetAuthCookie(txtUserName.Text, true);
System.Web.HttpCookie MyCookie = System.Web.Security.FormsAuthentication.GetAuthCookie(User.Identity.Name.ToString(), true);
MyCookie.Domain = "abc.com";
Response.AppendCookie(MyCookie);
Response.Redirect("Home.aspx", false);
Context.ApplicationInstance.CompleteRequest();
然后在两个应用程序的 home.aspx 页面中,我检查如下:
bool isLoggedIn = ((System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated);
if (!isLoggedIn)
{
FormsAuthentication.RedirectToLoginPage();
return;
}
在 web.config 中,我有以下设置:
<authentication mode="Forms">
<forms name="Authent" protection="All" timeout="60" loginUrl="Login.aspx" defaultUrl="Home.aspx" path="/" enableCrossAppRedirects="true" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
注意:我尝试给 cookie 的域名加上一个点 (.abc.com),但没有用。
我按如下方式解决了:
1) 在 web.config.
中添加域
<forms name="Authent" protection="All" timeout="525600" loginUrl="Login.aspx" defaultUrl="Home.aspx" path="/" enableCrossAppRedirects="true" slidingExpiration="true" domain=".abc" />
2) 我检查了是否通过以下行进行了身份验证:
if (!(Request.IsAuthenticated))
{
FormsAuthentication.RedirectToLoginPage();
return;
}
3) 在第一个块中,
MyCookie.Domain = ".abc.com"; // note the dot before domain name
我有两个子域,比如 b1.abc.com 和 s1.abc.com。我正在使用表单身份验证实现单点登录,但它似乎没有按预期工作。我想要的是,如果用户登录 b1.abc.com 然后打开 s1.abc.com 的主页(比如在另一个选项卡中),那么他不应该被重定向回登录页面,而是让他登录并给他看主页。
截至目前,当我登录 b1.abc.com 然后打开 s1.abc.com 时,它不会进行身份验证并重定向到登录页面。
下面是我的代码。
两个应用的登录按钮点击事件:
FormsAuthentication.SetAuthCookie(txtUserName.Text, true);
System.Web.HttpCookie MyCookie = System.Web.Security.FormsAuthentication.GetAuthCookie(User.Identity.Name.ToString(), true);
MyCookie.Domain = "abc.com";
Response.AppendCookie(MyCookie);
Response.Redirect("Home.aspx", false);
Context.ApplicationInstance.CompleteRequest();
然后在两个应用程序的 home.aspx 页面中,我检查如下:
bool isLoggedIn = ((System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated);
if (!isLoggedIn)
{
FormsAuthentication.RedirectToLoginPage();
return;
}
在 web.config 中,我有以下设置:
<authentication mode="Forms">
<forms name="Authent" protection="All" timeout="60" loginUrl="Login.aspx" defaultUrl="Home.aspx" path="/" enableCrossAppRedirects="true" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
注意:我尝试给 cookie 的域名加上一个点 (.abc.com),但没有用。
我按如下方式解决了:
1) 在 web.config.
中添加域<forms name="Authent" protection="All" timeout="525600" loginUrl="Login.aspx" defaultUrl="Home.aspx" path="/" enableCrossAppRedirects="true" slidingExpiration="true" domain=".abc" />
2) 我检查了是否通过以下行进行了身份验证:
if (!(Request.IsAuthenticated))
{
FormsAuthentication.RedirectToLoginPage();
return;
}
3) 在第一个块中,
MyCookie.Domain = ".abc.com"; // note the dot before domain name