簡體   English   中英

使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM