C# 在联合身份验证后获取用户声明,并将其包括在请求JWT中

C# 在联合身份验证后获取用户声明,并将其包括在请求JWT中,c#,azure-active-directory,openid-connect,ws-federation,C#,Azure Active Directory,Openid Connect,Ws Federation,我有一个基于Owin的Web应用程序和一个后端Web API,它们根据AAD进行身份验证,工作流可以如下所示 Web应用使用联合身份验证根据AAD对最终用户进行身份验证 Web应用程序从AAD请求JWT以访问后端Web API 用于验证最终用户的主代码 public void ConfigureAuth(IAppBuilder app) { // other code... app.UseWsFederationAuthentication(n

我有一个基于Owin的Web应用程序和一个后端Web API,它们根据AAD进行身份验证,工作流可以如下所示

  • Web应用使用联合身份验证根据AAD对最终用户进行身份验证
  • Web应用程序从AAD请求JWT以访问后端Web API
用于验证最终用户的主代码

    public void ConfigureAuth(IAppBuilder app)
    {
        // other code...
        app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
        {
            Wtrealm = realm,
            MetadataAddress = adfsMetadata
        });
    }
获取用于访问后端API的JWT的主要代码:

    internal async Task<string> GetAccessToken()
    {
        var authContext = new AuthenticationContext(authority);
        var credential = new ClientCredential(clientId, appKey);


        var result = await authContext.AcquireTokenAsync(apiId, credential);
        // Here, what I wanted is to use the other overloaded method
        // authContext.AcquireTokenAsync(apiId, credential, userAssertion);
        // But to instantiate a UserAssertion instance, the only way is 
        // to use the constructor new UserAssertion(assertionString)
        // and the assertionString should be in JWT format
        // unfortunately, the assertionString from Ws-Federation auth is
        // for sure in SAML2 format. So, the question is: 
        // Give I am using Ws-Federation auth protocal, How can I pass the
        // user information in requesting a JWT to backend API resource?

        return result.AccessToken;
    }
内部异步任务GetAccessToken()
{
var authContext=新的AuthenticationContext(授权);
var-credential=新的ClientCredential(clientId,appKey);
var result=await authContext.AcquireTokenAsync(APID,凭证);
//这里,我想要的是使用另一个重载方法
//AcquireTokenAsync(APID、凭证、用户断言);
//但要实例化UserAssertion实例,唯一的方法是
//使用构造函数newuserassertion(assertionString)
//断言字符串应该是JWT格式
//不幸的是,来自Ws-Federation auth的断言字符串是
//当然是SAML2格式。因此,问题是:
//假设我正在使用Ws-Federation身份验证协议,我如何通过
//向后端API资源请求JWT时的用户信息?
返回result.AccessToken;
}
一般来说,整个身份验证工作流程是正常的,我既可以对最终用户进行身份验证,也可以获取JWT以访问backedn API。但问题是JWT中没有最终用户声明。我确信我应该从联合身份验证结果中获得用户声明,然后将他们放在请求JWT的过程中。不幸的是,对于所有的方法、库和类,我都没有找到解决方案

顺便说一句,给出了一个如何获得包含最终用户声明的JWT的示例,但该解决方案不适用于我的场景,因为我使用的是联合身份验证,而不是OpenID Connect

编辑

为了澄清这个问题:在web应用程序中,我想使用方法
AuthenticationContext.AcquireTokenAsync
请求一个JWT令牌来访问后端web api

从我的演示代码中,您可以看到我正在使用
AcquireTokenAsync(apid,clientCredential)
verion重载。但是这个版本没有附上最终用户的声明。实际上,我需要的是
AcquireTokenAsync(apid、clientCredential、userAssertion)
重载方法

但是,要实例化一个
UserAssertion
,我需要用户断言字符串,它是来自用户身份验证结果的
AccessToken
。不幸的是,
UserAssertion
类只接受JWT格式的断言字符串,但是Ws-Federation身份验证返回SAML2格式的断言字符串,因此我无法实例化
UserAssertion
实例

因此,我的问题是:在使用Ws-Federation身份验证协议对最终用户进行身份验证的情况下,在后端,如何将用户断言信息(采用SAML2格式)传递给AAD,以请求后端api资源的JWT?

AAD提供“固定”声明。没有用于向令牌添加其他属性的声明规则

参考:


如果需要其他属性,则需要使用。

问题不是向令牌“添加”属性,而是如何传递用户声明的问题(声明信息位于Ws-Federation身份验证结果中的ClaimsPrincipal或SAML2类型断言字符串中)从AAD请求JWT令牌以访问后端Web API资源中的信息?有关更多信息,请参阅原始问题中的编辑部分。您找到解决方案了吗?