简体   繁体   English

使用asp.net的单点登录表单身份验证不起作用

[英]Single Sign On using asp.net forms authentication not working

I have two subdomains, say b1.abc.com and s1.abc.com . 我有两个子域,例如b1.abc.com和s1.abc.com。 I am implementing single sign on using forms authentication but it doesn't seem to work as expected. 我正在使用表单身份验证实现单点登录,但似乎无法按预期工作。 What I want is, if a user signin in b1.abc.com and then open home page of s1.abc.com (say in another tab), then he shouldn't be redirected back to login page, instead logged him in and show him home page. 我想要的是,如果用户在b1.abc.com中登录,然后打开s1.abc.com的主页(在另一个选项卡中说),则不应将他重定向回登录页面,而应将他登录并显示他的主页。

As of now, when I login in b1.abc.com and then open s1.abc.com, it doesn't authenticate and redirect to login page. 到目前为止,当我登录b1.abc.com并打开s1.abc.com时,它不进行身份验证并重定向到登录页面。

Below is my code. 下面是我的代码。

In login button click event of both the app : 在两个应用程序的登录按钮单击事件中:

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();

Then in home.aspx page of both the application, I check as below : 然后在两个应用程序的home.aspx页面中,我检查如下:

bool isLoggedIn = ((System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated);
if (!isLoggedIn)
{
    FormsAuthentication.RedirectToLoginPage();
    return;
}

In web.config, I have below settings : 在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>

NOTE : I tried giving domain name of cookie with a dot (.abc.com), but it didn't work. 注意:我尝试给cookie的域名加上一个点(.abc.com),但没有用。

I solved it as below : 我解决了如下问题:

1) Added domain in web.config. 1)在web.config中添加了域。

<forms name="Authent" protection="All" timeout="525600" loginUrl="Login.aspx" defaultUrl="Home.aspx" path="/" enableCrossAppRedirects="true" slidingExpiration="true" domain=".abc" />

2) I checked if authenticated by below lines : 2)我检查是否通过以下行进行了身份验证:

if (!(Request.IsAuthenticated))
{
    FormsAuthentication.RedirectToLoginPage();
    return;
}

3) In the first block in question, 3)在相关的第一个区块中,

MyCookie.Domain = ".abc.com"; // note the dot before domain name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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