如何在多种身份验证方案中授权用户? [英] How to authorize user in multiple authentication scheme?

查看:211
本文介绍了如何在多种身份验证方案中授权用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在.net核心应用程序中实现了多种身份验证方案.

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

推荐答案

您可以从中间件中的请求主体/标头中选择基于用户信息进行身份验证的方案:

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();
});

这篇关于如何在多种身份验证方案中授权用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆