如何在多重身份验证方案中授权用户?
How to authorize user in multiple authentication scheme?
我在我的 .net 核心应用程序下实施了多种身份验证方案。
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;
});
我想给特定用户授权特定方案
您可以在中间件中根据来自请求 body/header 的用户信息选择要进行身份验证的方案:
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();
});
我在我的 .net 核心应用程序下实施了多种身份验证方案。
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;
});
我想给特定用户授权特定方案
您可以在中间件中根据来自请求 body/header 的用户信息选择要进行身份验证的方案:
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();
});