How to authorize user in multiple authentication scheme?

风流意气都作罢 提交于 2020-03-05 04:27:12

问题


I have implemented multiple authentication scheme under my .net core application.

 services.AddAuthentication(
            sharedOptions =>
            {
                sharedOptions.DefaultScheme = Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultSignInScheme = Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddWsFederation("AuthenticationScheme1", options =>
            {
                options.Wtrealm = tenantList.Find(m => m.TenantID == 1).Wtrealm;
                options.MetadataAddress = tenantList.Find(m => m.TenantID == 1).MetadataAddress;
            })
            .AddWsFederation("AuthenticationScheme2", options =>
            {
                options.Wtrealm = tenantList.Find(m => m.TenantID == 2).Wtrealm;
                options.MetadataAddress = tenantList.Find(m => m.TenantID == 2).MetadataAddress;
            });

I want to authorize specific users with specific scheme


回答1:


You can choose the scheme want to authenticate based on user info from request body/header in a middleware :

app.Use(async (context, next) =>
{
    //read userinfo from request body or header

    if ("xxx".Equals("allen@xx.com"))
    {
        var result = await context.AuthenticateAsync("YourSchemeName");
        if (!result.Succeeded)
        {
            context.Response.StatusCode = 401;  
            return;
        }

    }
    ....

    await next();
});


来源:https://stackoverflow.com/questions/60414801/how-to-authorize-user-in-multiple-authentication-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!