Owin / Mvc添加了对承载令牌认证的支持

时间:2018-02-05 11:10:05

标签: asp.net owin openid adfs adal

我的目标是使用OpenId身份验证保护Asp.Net Mvc操作,并支持2种类型的客户端:浏览器和本机WPF应用程序。我将使用的STS是ADFS 2016。

目前客户端浏览器运行良好。为此,我在启动类中配置了 UseOpenIdConnectAuthentication 。 我可以调用我的Mvc操作(使用Authorize属性保护),用户被重定向到STS,一旦完成身份验证,我就会回到我的Mvc操作,并正确填写ClaimsIdentity。

现在我尝试让原生WPF应用程序能够在同一个Web应用程序中对同一个Mvc操作进行身份验证,事情变得棘手。 在客户端(我的WPF应用程序),我使用ADAL和以下代码:

        var authContext = new AuthenticationContext("<MySTSUri>");

        var authResult = await authContext.AcquireTokenAsync(
                             "http://localhost:1276/openid/login",
                             "MyNativeAppId",
                             new Uri("myapp://openid"),
                             new PlatformParameters(PromptBehavior.Auto),
                             UserIdentifier.AnyUser,
                             "");

        if (!string.IsNullOrEmpty(authResult.AccessToken))
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue(authResult.AccessTokenType, authResult.AccessToken);

                HttpResponseMessage response = await httpClient.GetAsync("http://localhost:1276/openid/login");

                if (response.IsSuccessStatusCode)
                {
                    var text = await response.Content.ReadAsStringAsync();
                }
            }
        }

问题基本上是我不能告诉Web应用程序能够验证这种类型的ADAL请求。

我在Web应用程序Owin启动文件配置中尝试了各种各样的事情:

  • 离开 UseOpenIdConnectAuthentication :它似乎不够,我已使用Web应用程序的ClientId重定向到STS
  • UseActiveDirectoryFederationServicesBearerAuthentication api,因为我知道我的STS将始终是ADFS
  • UseOAuthBearerAuthentication

他们都没有工作。

有人可以帮忙解决这个问题吗? 我是朝着正确的方向前进的吗?

非常感谢任何想法/指示。

谢谢, 亚历

1 个答案:

答案 0 :(得分:2)

我设法让它运转起来。我发布了答案的记录 帮助我很多的是在web.config中启用Owin日志:

  <system.diagnostics>
    <switches>
      <add name="Microsoft.Owin" value="Verbose" />
    </switches>
  </system.diagnostics>

然后使用Owin,您可以简单地链接多种身份验证方法。所以就我而言,我刚刚使用过:

app.UseActiveDirectoryFederationServicesBearerAuthentication(
                new ActiveDirectoryFederationServicesBearerAuthenticationOptions
                {
                    MetadataEndpoint = adfsMetadataEndpoint,
                    TokenValidationParameters = new TokenValidationParameters()
                    {
                        ValidAudiences = new[] { validAudience }
                    }
                });

app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AuthenticationType = "Cookies"
            });

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
            {
                AuthenticationType = "OpenId",

                ClientId = clientId,
                Authority = authority,
                RedirectUri = redirectUri,

                ResponseType = OpenIdConnectResponseTypes.CodeIdToken,

                Scope = "openid",

                SignInAsAuthenticationType = "Cookies"    
            });

干杯,
亚历

相关问题