2

在对 ADFS 进行身份验证后,一直在努力尝试提取不记名令牌...

我有一个 API 将接受承载令牌并针对 ADFS 对其进行验证。

我有一个 Web 表单 (.net 4.5.1) 应用程序,我正在修改它以使用 ADFS 3.0 来实现 SSO 身份验证。到目前为止,它针对 ADFS 服务器进行了正确的身份验证(显示到 ADFS 登录页面并登录)。

我的问题是我现在希望 WebForms 应用程序使用不记名令牌调用我的 Web API,我希望不记名令牌出现在 ADFS 返回的响应中,但它在哪里以及如何检索它?

我尝试使用 SecurityTokenValidated 和 SecurityTokenReceived WsFederationAuthenticationNotifications 事件,如下所示:

public void ConfigureAuth(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            app.UseWsFederationAuthentication(
                new WsFederationAuthenticationOptions
                {
                    AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType, // "WS-Fed Auth (Primary)",
                    Wtrealm = realm,
                    MetadataAddress = metadata,
                    Notifications = new WsFederationAuthenticationNotifications
                    {
                        AuthenticationFailed = context =>
                        {
                            context.HandleResponse();
                            context.Response.Redirect("Home/Error?message=" + context.Exception.Message);
                            return Task.FromResult(0);
                        },

                        SecurityTokenValidated = token =>
                            {
                                Token = token.AuthenticationTicket.ToString();
                                return Task.FromResult(0);
                            },

                        SecurityTokenReceived = token =>
                        {
                            Token = token.ToString();
                            return Task.FromResult(0);
                        }
                    }
                });
}

但是我在事件返回的对象中的任何地方都找不到令牌......我错过了什么?

感谢所有帮助。

4

1 回答 1

1

您作为 Web 登录的一部分收到的令牌不适合调用 Web API,原因有两个:A) 令牌的受众是 webform 应用程序,而 Web API 应该只接受受众对应的令牌Web API - 否则会在中间攻击中向人敞开大门,并且 B)您从 ADFS 获得的令牌是 SAML 令牌,它可能非常大,因此不适合包含在 HTTP 标头中(包含用于调用 Web API 的不记名令牌)。如果您决定忽略上述内容并使用该令牌 - 在使用用于 AzureBearerAuthentication 的 JWT 获取访问令牌中,您可以找到提取传入令牌位所需的代码。它适用于 openid connect 和 oauth 中间件。

于 2015-11-16T17:45:56.853 回答