Oauth 2.0 Azure Active Directory-获取访问令牌的授权

Oauth 2.0 Azure Active Directory-获取访问令牌的授权,oauth-2.0,azure-active-directory,authorization,Oauth 2.0,Azure Active Directory,Authorization,我希望能从我的web应用程序调用Microsoft Graph Api。在本文的第2节中,它解释了如何获取请求获取访问令牌所需的身份验证代码 作为第2部分请求的一部分,有人能告诉我从哪里获得“代码”吗?我希望它在重定向URL中作为查询字符串param返回,但事实并非如此 谢谢 编辑 我选择不使用MSAL,因为我在使用该库时遇到了一些错误。相反,我的配置如下: Startup.cs JwtSecurityTokenHandler.DefaultMapInboundClaims =

我希望能从我的web应用程序调用Microsoft Graph Api。在本文的第2节中,它解释了如何获取请求获取访问令牌所需的身份验证代码

作为第2部分请求的一部分,有人能告诉我从哪里获得“代码”吗?我希望它在重定向URL中作为查询字符串param返回,但事实并非如此

谢谢

编辑

我选择不使用MSAL,因为我在使用该库时遇到了一些错误。相反,我的配置如下:

Startup.cs

         JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

        var serviceProvider = services.BuildServiceProvider();
        var userAuthenticationTicketRepository = serviceProvider.GetService<IUserAuthenticationTicketRepositoryWrapper>();
        var configSettings = serviceProvider.GetService<IConfigSettings>();

        services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddAzureAd(options => configuration.Bind("Config:AzureAd", options))
            .AddCookie(options =>
            {
                options.SessionStore =
                    new AuthenticationTicketStore(userAuthenticationTicketRepository, configSettings);
            });
 public class ConfigureOidcOptions : IConfigureNamedOptions<OpenIdConnectOptions>
{
    private readonly AzureADOptions _azureOptions;

    public ConfigureOidcOptions(IOptions<AzureADOptions> azureOptions)
    {
        _azureOptions = azureOptions.Value;
    }

    public void Configure(string name, OpenIdConnectOptions options)
    {
        options.ClientId = _azureOptions.ClientId;
        options.ClientSecret = _azureOptions.ClientSecret;
        options.Authority = new Uri(new Uri(_azureOptions.Instance), _azureOptions.TenantId).ToString();
        options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.CallbackPath = _azureOptions.CallbackPath;
        options.UseTokenLifetime = true;
    }

    public void Configure(OpenIdConnectOptions options)
    {
        Configure(Options.DefaultName, options);
    }
}
JwtSecurityTokenHandler.DefaultMapInboundClaims=false;
var serviceProvider=services.BuildServiceProvider();
var userauthenticationticketpository=serviceProvider.GetService();
var configSettings=serviceProvider.GetService();
services.AddAuthentication(sharedOptions=>
{
sharedOptions.DefaultScheme=CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme=OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAd(options=>configuration.Bind(“Config:AzureAd”,options))
.AddCookie(选项=>
{
options.SessionStore=
新的AuthenticationTicketStore(UserAuthenticationTicketStore,配置设置);
});
添加azuread的实施

 public static AuthenticationBuilder AddAzureAd(this AuthenticationBuilder builder, Action<AzureADOptions> configureOptions)
    {
        builder.Services.Configure(configureOptions);
        builder.Services.AddSingleton<IConfigureOptions<OpenIdConnectOptions>, ConfigureOidcOptions>();

        builder.AddOpenIdConnect(options =>
        {
            options.Events = new OpenIdConnectEvents
            {
                OnRemoteFailure = context =>
                {
                    context.HandleResponse();
                    context.Response.Redirect("Account/AccessDenied");

                    return Task.FromResult(0);
                }
            };

        });

        return builder;
    }
public static AuthenticationBuilder AddAzureAd(此AuthenticationBuilder,操作配置选项)
{
builder.Services.Configure(配置选项);
builder.Services.AddSingleton();
builder.AddOpenIdConnect(选项=>
{
options.Events=新的OpenIdConnectEvents
{
OnRemoteFailure=上下文=>
{
context.HandleResponse();
context.Response.Redirect(“帐户/访问被拒绝”);
返回Task.FromResult(0);
}
};
});
返回生成器;
}
configureoidcopions

         JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

        var serviceProvider = services.BuildServiceProvider();
        var userAuthenticationTicketRepository = serviceProvider.GetService<IUserAuthenticationTicketRepositoryWrapper>();
        var configSettings = serviceProvider.GetService<IConfigSettings>();

        services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddAzureAd(options => configuration.Bind("Config:AzureAd", options))
            .AddCookie(options =>
            {
                options.SessionStore =
                    new AuthenticationTicketStore(userAuthenticationTicketRepository, configSettings);
            });
 public class ConfigureOidcOptions : IConfigureNamedOptions<OpenIdConnectOptions>
{
    private readonly AzureADOptions _azureOptions;

    public ConfigureOidcOptions(IOptions<AzureADOptions> azureOptions)
    {
        _azureOptions = azureOptions.Value;
    }

    public void Configure(string name, OpenIdConnectOptions options)
    {
        options.ClientId = _azureOptions.ClientId;
        options.ClientSecret = _azureOptions.ClientSecret;
        options.Authority = new Uri(new Uri(_azureOptions.Instance), _azureOptions.TenantId).ToString();
        options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.CallbackPath = _azureOptions.CallbackPath;
        options.UseTokenLifetime = true;
    }

    public void Configure(OpenIdConnectOptions options)
    {
        Configure(Options.DefaultName, options);
    }
}
public类configureoidcopions:IConfigureNamedOptions
{
私人只读AzureADOptions _azureOptions;
公共配置配置配置(IOptions azureOptions)
{
_azureOptions=azureOptions.Value;
}
public void配置(字符串名称、OpenIdConnectOptions选项)
{
options.ClientId=\u azureOptions.ClientId;
options.ClientSecret=\u azureOptions.ClientSecret;
options.Authority=newURI(newURI(_-azureOptions.Instance),_-azureOptions.TenantId.ToString();
options.signnscheme=CookieAuthenticationDefaults.AuthenticationScheme;
options.CallbackPath=\u azureOptions.CallbackPath;
options.UseTokenLifetime=true;
}
public void配置(OpenIdConnectOptions选项)
{
配置(Options.DefaultName,Options);
}
}

授权请求应为

https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?
client_id={client_id}
&response_type=code
&redirect_uri=http://localhost/myapp/
&response_mode=query
&scope=offline_access user.read mail.read
&state=12345
租户客户id替换为您的价值。重定向uri应与门户中的一致

当您在浏览器中请求url时,系统将要求您登录。之后,您将在url中获得代码参数


是的,您可以将
code
作为重定向URL的查询参数。如果您没有得到它,您应该得到一个
错误
参数。无论如何,发布您的身份验证请求和您得到的响应URL会很好。我认为“授权”步骤是在提示我登录我的web应用程序时进行的,对吗。我没有得到'code'参数,但也没有得到错误。如何发布我的身份验证请求?编辑问题并添加您的身份验证请求URL和响应URL(如果不想公开,可以屏蔽client_id param值)。通过授权步骤,它们可能意味着客户机(您的应用程序)获得了用户(资源所有者)的授权,可以代表用户使用资源(MS Graph API)——OAuth2是一种访问委派协议。在代码中的某一点上,我调用了authorize端点。当我将“Authorize”属性添加到我的MVC控制器时,这不是已经得到了控制吗?@japesophey如果您使用的是adal或msal,那么它当然已经得到了控制。@japesophey您可以看看这个示例。由于我遇到了错误,Hi选择了不使用msal。我已经用我的代码片段更新了我的问题。@japesophey现在你的问题是如何使用adal获取访问图api的访问令牌?