首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ADFS4.0中使用client_credentials流将返回401

在ADFS4.0中使用client_credentials流将返回401
EN

Stack Overflow用户
提问于 2017-03-15 04:10:57
回答 2查看 952关注 0票数 0

我有一个“服务器应用程序访问web API”的场景。

该网站使用OIDC并进行身份验证,没有问题。

然而,我有一个在没有用户上下文的情况下访问一些web API的用例,为此,我使用了client_credentials。

服务器应用程序具有客户端ID和密钥。

因此,假设web API URL为:

https://my-pc/WebService/api/my-api/

web API具有RP标识符:

https://my-pc/WebService/api/my-api/

访问控制策略为:

允许所有人

我有一个声明规则:

c:[]索赔问题( => = c);

客户端权限设置为:

“所有客户端”,范围为openid和user_impersonation。

代码是:

代码语言:javascript
复制
AuthenticationContext ac = new AuthenticationContext("https://my-adfs/adfs/", false);
// ClientCredential contains client_id and secret key 
AuthenticationResult result = await ac.AcquireTokenAsync("https://my-pc/WebService/api/my-api/", clientCredential);

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://my-pc/WebService/api/my-api/");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);

HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("foo", "blah"), new KeyValuePair<string, string>("foo1", "blah1") });
request.Content = content;
HttpResponseMessage response = await client.SendAsync(request);

ADFS返回访问令牌没有问题,但当我调用web api时,我总是得到一个401 - Unauthenticated。

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2017-03-16 03:05:29

最终解决了这个问题,并且wrote it up

这两段代码:

代码语言:javascript
复制
using Microsoft.IdentityModel.Clients.ActiveDirectory;

ClientCredential clientCredential = new ClientCredential(clientId, secretKey);

AuthenticationContext ac = new AuthenticationContext("https://my-adfs/adfs/", false);
AuthenticationResult result = await ac.AcquireTokenAsync("https://my-pc/WebService", 

clientCredential);

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,  

"https://my-pc/WebService/api/my-api/");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);

HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string,  

string>("foo", "blah"), new KeyValuePair<string, string>("foo1", "blah1") });
request.Content = content;
HttpResponseMessage response = await client.SendAsync(request);

if (response.IsSuccessStatusCode)
    // etc

在Startup.Auth.cs中

代码语言:javascript
复制
app.UseActiveDirectoryFederationServicesBearerAuthentication(
    new ActiveDirectoryFederationServicesBearerAuthenticationOptions
    {
        TokenValidationParameters = new TokenValidationParameters()
        {
            SaveSigninToken = true,
            ValidAudience = "https://my-pc/WebService"
        },

        MetadataEndpoint = "https://my-adfs/FederationMetadata/2007-06/FederationMetadata.xml"
});

花了我好长时间!

票数 1
EN

Stack Overflow用户

发布于 2017-03-15 23:29:19

由于您没有在访问令牌中获取用户主体,因此您没有将用户会话附加到主体。当Web API在cookie(或Authorization header,如果您正在发送访问令牌)中查找主体时,它找不到主体,您仍然是匿名用户。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42795633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档