【发布时间】:2022-04-21 08:30:02
【问题描述】:
我有一个使用 Ws-Federation 身份验证和 SSO 应用程序(不是 ADFS)的 Owin 应用程序。每当我的 Owin 应用程序收到请求时,它首先通过检查它是否具有正确的 cookie 来对用户进行身份验证,然后从中构建声明和身份验证票证。如果它没有正确的 cookie,它会重定向到 STS,STS 会传回可用于完成身份验证的 SAML 令牌。
除一部分外,所有这些都有效。在收到并验证令牌后,由于某种原因,它会重定向回 STS,从而创建一个无限循环。我很确定这是因为我的一个或多个配置值是错误的,因为不清楚每个属性的用途以及是否需要它。我在下面复制了我的配置代码:
public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType( CookieAuthenticationDefaults.AuthenticationType);
CookieAuthenticationOptions cookieOptions = new CookieAuthenticationOptions
{
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
CookieName = "MyCookie",
CookiePath = "/CookiePath",
AuthenticationMode = AuthenticationMode.Active
};
// Basically the same as saying app.UseCookieAuthentication(app, cookieOptions)
app.Use(typeof(MyCustomCookieAuthenticationMiddleware), app, cookieOptions);
// Define properties for WsFederationAuthenticationOptions
var config = new Microsoft.IdentityModel.Protocols.WsFederationConfiguration
{
Issuer = "https://sts-domain.com/STS/",
TokenEndpoint = this.owinServerUrl // I don't know what this should be. I just made it the same as my owin start url
};
Saml2SecurityTokenHandler handler = new Saml2SecurityTokenHandler
{
Configuration = new SecurityTokenHandlerConfiguration
{
IssuerTokenResolver = new MyCustomSecurityTokenResolver
{
Thumbprint = somePublicKeyStr,
StoreLocation = System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine,
StoreName = "My"
}
}
};
var handlers = new SecurityTokenHandlerCollection(new List<SecurityTokenHandler>() { handler });
var wsFedOptions = new WsFederationAuthenticationOptions
{
AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType,
AuthenticationMode = AuthenticationMode.Passive,
SignInAsAuthenticationType = CookieAuthenticationDefaults.AuthenticationType, // I'm not sure what this is exactly, but I think I've seen this used in examples
Configuration = config,
Wtrealm = this.owinServerUrl, // I'm guessing what this should be
Wreply = someString, // I have no idea what this should be -- it hasn't seemed to have any effect so far
SecurityTokenHandlers = handlers,
TokenValidationParameters = new TokenValidationParameters
{
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType, // I'm not sure whether this should be cookie or ws federation, but I don't think it's relevant to my problem
ValidIssuer = "https://sts-domain.com/STS/", // same as config.Issuer above
}
};
app.UseWsFederationAuthentication(wsFedOptions);
AuthenticateAllRequests(app, WsFederationAuthenticationDefaults.AuthenticationType);
app.Use<MyCustomMiddleware>();
}
private static void AuthenticateAllRequests(IAppBuilder app, params string[] authenticationTypes)
{
app.Use((context, continuation) =>
{
if (context.Authentication.User?.Identity?.IsAuthenticated ?? false)
{
return continuation();
}
else
{
context.Authentication.Challenge(authenticationTypes);
return Task.CompletedTask;
}
});
}
正如我在我的 cmets 代码中指出的那样,有一些属性我不确定。不幸的是,WsFederationAuthenticationOptions 的文档对我帮助不大。例如,我知道Wtrealm 和Wreply 很重要(也许Wreply 不太重要),但所有文档都说“获取或设置'wtrealm'”和“获取或设置'wreply'”。我发现this thread 有解释:
wtrealm 是一个标识 RP 的 URI(不一定是 URL)。 STS 使用它来决定是否发布令牌以及声明要给予它的内容。
wreply 是 RP 希望使用生成的令牌重定向到的 URL。 STS 不一定要遵守此请求……有时 STS 有一个预定义的地址,它将根据已建立的信任重定向到。至少,STS 应该拒绝重定向到与其关联的域不同的域。否则,该请求可能会将用户发送到恶意网站。
这是有道理的,除了当我一直在测试我的 owin 应用程序时,Wreply 似乎对 STS 的重定向位置没有影响以传回令牌;我为 Wtrealm 设置的 URL 决定了这一点。
我想做的就是让 STS 传回令牌,验证用户身份,然后继续执行用户指定的启动所有这一切的路由。我不确定这是否相关,但我也认为应该在 STS 传回令牌时设置 cookie。如果是这种情况,则不会发生无限重定向,因为当它返回进行身份验证时,cookie 身份验证会找到 cookie,并且应用程序将正常运行。
更新 1
我更改了一些值并收到了不同的错误消息,所以我想我会在这里分享它们,以防它们有助于阐明可能发生的情况。正如您从我的帖子中看到的那样,我不想分享有关该应用程序的真实信息,所以请多多包涵。假设总体 Web 应用程序(包含我的 owin 应用程序以及其他一些东西)具有 URL http://localhost/app。我的 owin 应用程序有服务器 url(我在上面的代码中称之为 this.owinServerUrl)http://localhost/app/owin。
- 当我创建
WsFederationConfiguration.TokenEndpoint = "http://localhost/app"时,我得到了无限重定向。当我设置为"http://localhost/app/owin"时,我没有得到无限重定向,但我确实得到了另一个错误(我得到的错误取决于其他值,我现在将解释)。 - 我弄错了——
Wreply确实似乎有效果。当我没有设置Wreply时,我得到一个 414 错误:请求 URL 太长。当我设置它时(作为任何字符串,无论是我认为有意义的 URL 还是胡言乱语),我收到 400 bad request: request too long。
【问题讨论】:
标签: c# authentication cookies owin ws-federation