如何在AuthorizationServiceCollectionExtensions的AddAuthorization()中插入我自己的逻辑?

时间:2018-11-02 16:05:37

标签: c# .net-core authorization microsoft-graph

我正在为.net核心项目开发基于声明的授权解决方案。我从遵循Microsoft.Graph示例(https://github.com/microsoftgraph/aspnetcore-connect-sample)开始。这很有帮助。

我想将自己的逻辑插入AddAuthorization()方法。 我知道我可以像在Starup.cs文件中那样简单地添加逻辑:

services.AddAuthorization(options =>
        {
            //Add own logic
        });

在上述示例中,它们与AzureAdAuthenticationBuilderExtensions类相似。在该课程中,我添加了自己的逻辑。

public static class AzureAdAuthenticationBuilderExtensions
{
    public static AuthenticationBuilder AddAzureAd(this AuthenticationBuilder builder)
        => builder.AddAzureAd(_ => { });

    public static AuthenticationBuilder AddAzureAd(this AuthenticationBuilder builder, Action<AzureAdOptions> configureOptions)
    {
        builder.Services.Configure(configureOptions);
        builder.Services.AddSingleton<IConfigureOptions<OpenIdConnectOptions>, ConfigureAzureOptions>();
        builder.AddOpenIdConnect();
        return builder;
    }

    private class ConfigureAzureOptions : IConfigureNamedOptions<OpenIdConnectOptions>
    {
        private readonly AzureAdOptions _azureOptions;

        public ConfigureAzureOptions(IOptions<AzureAdOptions> azureOptions)
        {
            _azureOptions = azureOptions.Value;
        }

        public void Configure(string name, OpenIdConnectOptions options)
        {
            options.ClientId = _azureOptions.ClientId;
            options.Authority = $"{_azureOptions.Instance}{_azureOptions.TenantId}";
            options.UseTokenLifetime = true;
            options.CallbackPath = _azureOptions.CallbackPath;
            options.RequireHttpsMetadata = false;


            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = false,
            };

            options.Events = new OpenIdConnectEvents
            {
                OnAuthenticationFailed = context =>
                {
                    context.Response.Redirect("/Home/Error");
                    context.HandleResponse();
                    return Task.CompletedTask;
                },
                OnTokenValidated = context =>
                {
                    //New logic I have obscured for clarity. 
                    //Here I add claims to the ClaimsIdentity object.

                    return Task.FromResult(0);
                }
            };

        }

        public void Configure(OpenIdConnectOptions options)
        {
            Configure(Options.DefaultName, options);
        }
    }
}

我想覆盖(不确定我是否使用正确的词)startup.cs中IServiceCollection对象的AddAuthorization()。

我从这里开始,但是我不确定从这里去哪里

    public static class AuthorizationServiceCollectionExtensionsTest
{
    public static IServiceCollection AddAuthorization(this IServiceCollection services)
        => services.AddAuthorization(_ => { });

    public static IServiceCollection AddAuthorization(this IServiceCollection services, Action<AuthorizationOptions> configure)
    {
        services.Configure(configure);
        //add singleton? then I add that class below?
        return services.AddAuthorization();
    }
}

另外,还有一个新的问题:应用程序如何知道如何查看我正在创建的这些新Extensions类的逻辑?

谢谢, 约翰

0 个答案:

没有答案