我已经正确设置了活动目录,并且可以使用以下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页。我想念什么吗?我希望用户登录(如果尚未登录),但是我不知道为什么应用程序不重定向到登录。任何帮助将不胜感激。
答案 0 :(得分:1)
经过几天的尝试,我找到了解决方案。我将此代码添加到Default.aspx page_load方法的顶部,以便在未登录的情况下运行此代码:
if (!System.Web.HttpContext.Current.Request.IsAuthenticated)
{
System.Web.HttpContext.Current.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" },
WsFederationAuthenticationDefaults.AuthenticationType);
}