【问题标题】:Writing Bearer Token to Javascript将不记名令牌写入 Javascript
【发布时间】:2015-12-04 14:03:30
【问题描述】:

我正在尝试进行概念验证。我正在使用 Azure Active Directory 并尝试在旧项目中实现 OAuth。

这个项目一半是使用Web Forms,另一半是直接通过javascript调用另一个项目中的WebAPI。

作为测试,我通过 UseOpenIdConnectAuthentication 的 AuthorizationCodeReceived 通知事件获取 Bearer Token。我快速将令牌写入使用以下代码调用 WebAPI 的页面:

 $.ajax({
                        url: baseVotingHeaderURL,
                        type: 'GET',
                        dataType: "json",
                        beforeSend: function(xhr){
                            xhr.setRequestHeader('Authorization', 'Bearer ' + XXXXXXXXXXXXXXXXX);
                        },
                        success: function(result) {
                            options.success(result);
                        },
                        error: function(err) {
                            options.error(err);
                        }
                    });

我可以在 Fiddler 中看到令牌正在传递:

不存在代理授权标头。 存在授权标头:Bearer XXXXXXXXXXXXXXXX(我显然已将令牌替换为 X)

我仍然收到未经授权的 401。

为什么这不起作用?

下面是 Startup.Auth.cs 的代码

app.SetDefaultSignInAsAuthenticationType( CookieAuthenticationDefaults.AuthenticationType );
app.UseCookieAuthentication( new CookieAuthenticationOptions( ) );

app.UseWindowsAzureActiveDirectoryBearerAuthentication( new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
    Tenant = "XXXXXX.onmicrosoft.com",
    AuthenticationType = "OAuth2Bearer",
    TokenValidationParameters = new TokenValidationParameters( )
    {
        ValidAudience = "https://XXXX.onmicrosoft.com/XXXXX"
    }
} );

app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
            Authority = "https://login.microsoftonline.com/XXXXX.onmicrosoft.com",
            PostLogoutRedirectUri = "https://XXXXXXX/gbl/Home.aspx",
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthenticationFailed = context =>
                {
                    context.HandleResponse( );
                    context.Response.Redirect( "/Error?message=" + context.Exception.Message );
                    return Task.FromResult( 0 );
                },              
                AuthorizationCodeReceived = context =>
                {
                    var client = ClientId;
                    var key = "XXXXXXXXXXXXXXXXXXX=";                               

                    var credential = new ClientCredential( client, key );
                    var authority = String.Format( CultureInfo.InvariantCulture, @"https://login.microsoftonline.com/{0}", "XXXXX.onmicrosoft.com" );
                    var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext( authority );

                    Uri redirectUri = new Uri( HttpContext.Current.Request.Url.GetLeftPart( UriPartial.Path ) );
                    var apiResourceId = "https://graph.windows.net";
                    AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                            context.Code, redirectUri, credential, apiResourceId );

                    EndpointAndTokenHelper.DecodeAndWrite( result.AccessToken );
                    System.Diagnostics.Debug.WriteLine( result.AccessToken );

                    return Task.FromResult( 0 );
                }
            }
        } );
}

【问题讨论】:

    标签: oauth-2.0 azure-active-directory openid-connect bearer-token


    【解决方案1】:

    我逐字逐句地按照示例进行操作。但是,authContext.AquireTokenByAuthorizationCode 中的最后一个参数应该是我的 WebAPI 资源,而不是 https://graph.windows.net

    我不知道为什么示例使用https://graph.windows.net

    【讨论】:

    • 示例使用图表,因为这是他们想要调用的 API。
    • 我认为 Graph 是 Azure AD 中的另一个端点,需要它来查询有关用户的更多信息。
    • 它是 - 但因此,它是一种需要访问令牌的资源:请求该令牌遵循适用于受 Azure AD 保护的所有其他 API 的相同“物理定律”,并且包括指定资源标识符
    • 啊……明白了!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2021-10-04
    • 2014-06-10
    • 2017-10-27
    相关资源
    最近更新 更多