Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用Asp.Net Web API中的代表流调用graphAPI_C#_Microsoft Graph Api_Msal_Asp.net5 - Fatal编程技术网

C# 无法使用Asp.Net Web API中的代表流调用graphAPI

C# 无法使用Asp.Net Web API中的代表流调用graphAPI,c#,microsoft-graph-api,msal,asp.net5,C#,Microsoft Graph Api,Msal,Asp.net5,我正试图在Asp.net Web API(.net 5)中代表用户实现。我从移动应用程序收到访问令牌,并将其发送到我的Web API。Web API使用此令牌调用GRAPH API以获取用户的配置文件详细信息。 下面是我的代码 Startup.cs文件 services.AddAuthentication("JwtBearer") .AddJwtBearer("JwtBearer", options =&g

我正试图在Asp.net Web API(.net 5)中代表用户实现。我从移动应用程序收到访问令牌,并将其发送到我的Web API。Web API使用此令牌调用GRAPH API以获取用户的配置文件详细信息。 下面是我的代码 Startup.cs文件

 services.AddAuthentication("JwtBearer")
                        .AddJwtBearer("JwtBearer", options =>
                        {
                            options.MetadataAddress = $"https://login.microsoftonline.com/{Configuration["b2bAzureAppIdentity:TenantId"]}/v2.0/.well-known/openid-configuration";
                            options.TokenValidationParameters = new TokenValidationParameters()
                            {
                                ValidIssuer = $"https://sts.windows.net/{Configuration["b2bAzureAppIdentity:TenantId"]}/",

                                // as audience, both the client id and the identifierUri are allowed (sematically equivalent)
                                ValidAudiences = new[] { Configuration["b2bAzureAppIdentity:AppIdUri"], Configuration["b2bAzureAppIdentity:ClientId"] }
                            };
                        }).AddMicrosoftIdentityWebApi(Configuration, "b2bAzureAppIdentity")
                        .EnableTokenAcquisitionToCallDownstreamApi()
                        .AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
                        .AddInMemoryTokenCaches();
控制器

 [HttpGet("GetMyDetails")]
    [AuthorizeForScopes(Scopes = new string[] { "user.read" })]
    public async Task<IActionResult> GetMyDetails()
    {
        var user = await _graphServiceClient.Me.Request().GetAsync();
        return new OkObjectResult(user.Photo);
    }
在Azure中,API权限和作用域设置正确,这是显而易见的,因为当我从postman处调用时,我能够代表获取accesstoken,并使用它通过调用获取用户的配置文件详细信息

在这一行的控制器中

var user = await _graphServiceClient.Me.Request().GetAsync();
我收到一个错误:“没有向AcquireTokenSilent调用传递帐户或登录提示。”

我在谷歌上搜索了这个错误,解决方案说用户应该同意这个范围,但是它已经得到Azure门户管理员的同意。此外,这在Postman中起作用的事实使我们相信应用程序和API的配置是正确的。


有人遇到过类似的问题吗?

这是因为接收到的访问令牌没有与获取用户详细信息的请求一起发送。下面是一个如何实施的示例:

在这种情况下,令牌被添加到调用
WithUserAssertion
的请求中


请让我知道这是否有帮助,如果您还有其他问题。

这对我很有用,非常感谢:)
var user = await _graphServiceClient.Me.Request().GetAsync();
// Create a client application.
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create(clientId)
                .WithTenantId(tenantID)
                // The Authority is a required parameter when your application is configured 
                // to accept authentications only from the tenant where it is registered.
                .WithAuthority(authority)
                .WithClientSecret(clientSecret)
                .Build();
                
// Use the API reference to determine which scopes are appropriate for your API request.
// e.g. - https://docs.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http
var scopes = new string[] { "User.Read" };
// Create an authentication provider.
ClientCredentialProvider authenticationProvider = new OnBehalfOfProvider(confidentialClientApplication, scopes);

var jsonWebToken = actionContext.Request.Headers.Authorization.Parameter;
var userAssertion = new UserAssertion(jsonWebToken);
// Configure GraphServiceClient with provider.
GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);
// Make a request
var me = await graphServiceClient.Me.Request().WithUserAssertion(userAssertion).GetAsync();