gpt4 book ai didi

c# - 具有 WsFederation 身份验证的 Owin 应用陷入无限重定向循环

转载 作者:行者123 更新时间:2023-12-05 07:39:31 26 4
gpt4 key购买 nike

我有一个 Owin 应用程序,它通过 SSO 应用程序(不是 ADFS)使用 Ws-Federation 身份验证。每当我的 Owin 应用程序收到请求时,它都会首先检查它是否具有正确的 cookie,然后从中构建声明和身份验证票证,从而对用户进行身份验证。如果它没有正确的 cookie,它会重定向到 STS,STS 传回可用于完成身份验证的 SAML token 。

除一部分外,所有这些都有效。在收到并验证 token 后,由于某种原因它会重定向回 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;
}
});
}

正如我在代码和注释中指出的那样,有些属性我不确定。不幸的是,WsFederationAuthenticationOptions 的文档对我帮助不大。例如,我知道 WtrealmWreply 很重要(也许 Wreply 不那么重要),但所有文档都说“获取或设置'wtrealm'”和“获取或设置 'wreply'。”我找到了 this thread有一个解释:

wtrealm is a URI (not necessarily a URL) that identifies the RP. The STS uses this to decide whether to issue a token and what claims to give it.

wreply is the URL that the RP would like to be redirected to with the resulting token. The STS is not bound to comply with this request... sometimes the STS has a predefined address it will redirect to based on the established trust. At the very least, the STS should refuse to redirect to a different domain than the one it associates with the realm. Otherwise, the request could be a vector to send the user to a malicious site.

这是有道理的,除了当我一直在测试我的 owin 应用程序时,Wreply 似乎对重定向 STS 以传回 token 的位置没有影响;我为 Wtrealm 设置的 URL 决定了这一点。

我想要做的就是让 STS 传回 token ,对用户进行身份验证,然后继续执行用户指定的启动所有这一切的路由。我不确定这是否相关,但我还认为应该在 STS 传回 token 时设置 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 错误请求:请求太长。

最佳答案

不就是因为你的AuthenticateAllRequests函数吗?我认为您认为它仅在用户通过身份验证并设置 context.Authentication.User?.Identity?.IsAuthenticated 后运行。但是,我认为它在用户通过身份验证之后和 context.Authentication.User?.Identity?.IsAuthenticated 设置之前运行。由于从未设置,它是否只是通过调用“context.Authentication.Challenge(authenticationTypes)”再次调用身份验证?

关于c# - 具有 WsFederation 身份验证的 Owin 应用陷入无限重定向循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47066326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com