[英]Integrating ADFS SSO into existing .net web application
我的活動目錄設置正確,我可以使用如下所示的 URL 轉到 IDP 登錄頁面:
https://SERVER/adfs/ls/idpinitiatedsignon.htm
我創建了一個新項目,並且能夠通過該應用程序進行簡單的登錄。
現在我正在嘗試將其實現到當前的 Web 應用程序代碼中。
在簡單的項目中,下面的代碼運行后,它會重定向到 IDP 登錄頁面。
public class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
private static string realm = ConfigurationManager.AppSettings["ida:Wtrealm"];
private static string adfsMetadata = ConfigurationManager.AppSettings["ida:ADFSMetadata"];
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseWsFederationAuthentication(
new WsFederationAuthenticationOptions
{
Wtrealm = realm,
MetadataAddress = adfsMetadata
});
}
}
當此代碼在現有項目中運行時,它永遠不會重定向,而是繼續加載 Default.aspx 頁面。 我錯過了什么嗎? 如果用戶還沒有登錄,我希望用戶登錄,但我無法弄清楚為什么應用程序沒有重定向到登錄。任何幫助將不勝感激。
經過幾天的嘗試,我找到了解決方案。 我將此代碼添加到 Default.aspx page_load 方法的頂部以在未登錄時運行此代碼:
if (!System.Web.HttpContext.Current.Request.IsAuthenticated)
{
System.Web.HttpContext.Current.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" },
WsFederationAuthenticationDefaults.AuthenticationType);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.