【问题标题】:Asp.Net Core | Extend windows auth identity objectAsp.Net 核心 |扩展 windows auth 身份对象
【发布时间】:2018-06-26 16:05:27
【问题描述】:

我想在我的 Intranet 应用程序中使用 Windows Auth,但我需要扩展标识对象以获取一些额外的数据。截至目前,我只能访问身份用户中的域名。我尝试实现自己的用户/角色存储以拦截授权调用,然后使用域名访问我们的数据库并获取额外的数据。我实现了自己的商店,但似乎没有调用任何方法。当应用程序授权窗口用户时如何拦截,以便我可以转到我们的数据库并获取我需要放入用户对象的内容?

这是我的 Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddAuthentication(IISDefaults.AuthenticationScheme);
    services.AddIdentity<MyUser, IdentityRole>()
         .AddUserStore<MyUserStore>()
         .AddRoleStore<MyRoleStore>()
         .AddDefaultTokenProviders();

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseAuthentication();
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();

    app.UseMvc();
}

【问题讨论】:

    标签: c# asp.net-core windows-authentication asp.net-core-identity


    【解决方案1】:

    我所做的是从 MVC 中删除基本身份验证,并添加了扩展 AuthenticationService 的 AuthenticationHandler,因为我不想重新发明 IAuthenticationService 中的每个方法,所以:

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
    
    services.AddAuthentication(IISDefaults.AuthenticationScheme);
    services.AddIdentity<MyUser, IdentityRole>()
         .AddUserStore<MyUserStore>()
         .AddRoleStore<MyRoleStore>()
         .AddDefaultTokenProviders();
    
    services.Remove(services.FirstOrDefault(x => x.ServiceType == typeof(IAuthenticationService)));
    services.Add(new ServiceDescriptor(typeof(IAuthenticationService),typeof(AuthenticationHandler), ServiceLifetime.Scoped));
    
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }
    

    然后

    public class AuthenticationHandler : AuthenticationService
        {
            private readonly ILdapRepository _ldapRepository;
            public AuthenticationHandler(ILdapRepository ldapRepository,
                IAuthenticationSchemeProvider schemes, IAuthenticationHandlerProvider handlers,
                IClaimsTransformation transform) : base(schemes, handlers, transform)
            {
                _ldapRepository = ldapRepository;
            }
            public async override Task<AuthenticateResult> AuthenticateAsync(HttpContext context, string scheme)
            {
                var idk = await base.AuthenticateAsync(context, scheme);
                if (idk.Succeeded) {
                    var claims = _ldapRepository.LoadClaimsFromActiveDirectory(idk.Principal.Claims.FirstOrDefault(x => x.Type == CustomClaimTypes.Name)?.Value);
                    idk.Principal.AddIdentity(claims);
                }
                return idk;
            }
    }
    

    LdapRepository 只是 Active Directory 类的 DirectoryEntry 和 DirectorySearcher。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-28
      • 2019-03-24
      • 2019-02-19
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 2018-10-11
      • 2021-02-06
      相关资源
      最近更新 更多