繁体   English   中英

无法从httpcontext asp.net核心获取“ access_token”

[英]Cant get 'access_token' from httpcontext asp.net core

我在Asp.Net Core 2.0项目中从httpContext获取令牌时遇到问题。 我在前端获得了隐式的ADAL授权,在这里获取令牌并在访问API时将其发送到标头中。 身份验证进行得很好,但是当我想获取对Microsoft Graph api的请求的令牌时,我会得到null

我的Startup身份验证部分:

services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(options =>
        {
            AuthenticationOptions authSettings = Configuration.GetSection("Authentication").Get<AuthenticationOptions>();
            options.TokenValidationParameters = new TokenValidationParameters()
            {
                ValidateIssuer = false,
                ValidAudiences = new List<string> { authSettings.ClientId, authSettings.AppIdUri }
            };
            options.Authority = "https://login.microsoftonline.com/common";
            options.Audience = "**";
            options.TokenValidationParameters.ValidateLifetime = true;
            options.TokenValidationParameters.ClockSkew = TimeSpan.Zero;

            options.SaveToken = true;
        });

        services.AddAuthorization();

        services.AddMvc();

我需要获得令牌的方法:

public async Task AuthenticateRequestAsync(HttpRequestMessage request)
    {
        var httpContext = _httpContextAccessor.HttpContext;

        //Get the access token used to call this API
        string token = await httpContext.GetTokenAsync("access_token");

        //We are passing an *assertion* to Azure AD about the current user
        //Here we specify that assertion's type, that is a JWT Bearer token
        string assertionType = "urn:ietf:params:oauth:grant-type:jwt-bearer";

        //User name is needed here only for ADAL, it is not passed to AAD
        //ADAL uses it to find a token in the cache if available
        var user = httpContext.User;
        string userName = user.FindFirstValue(ClaimTypes.Upn) ?? user.FindFirstValue(ClaimTypes.Email);

        var userAssertion = new UserAssertion(token, assertionType, userName);

        //Construct the token cache
        var cache = new DistributedTokenCache(user, _distributedCache, _loggerFactory, _dataProtectionProvider);

        var authContext = new AuthenticationContext(_authSettings.Authority, cache);
        var clientCredential = new ClientCredential(_authSettings.ClientId, _authSettings.ClientSecret);
        //Acquire access token
        var result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", clientCredential, userAssertion);
        //Set the authentication header
        request.Headers.Authorization = new AuthenticationHeaderValue(result.AccessTokenType, result.AccessToken);
    }

await httpContext.GetTokenAsync("access_token");的部分await httpContext.GetTokenAsync("access_token"); 它返回null

非常抱歉,解决方案很容易。 并请您注意。

httpContext.Request.Headers.TryGetValue("Authorization", out var token);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM