c# - 基于主机名的 ASP.NET Core 2.0 身份验证
问题描述
我会说带有身份验证的经典 ASP.NET Core 2.0 应用程序包括在Startup.cs文件的ConfigureServices
方法中添加所需的身份验证服务:
services.AddAuthentication().AddFacebook(facebookOptions =>
{
facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
});
只要在ConfigurationServices
调用方法时身份验证配置是已知的并且对于所有请求都是相同的,这很好。
我们的案例需要不同的身份验证配置,比如说基于主机名:
company1.example.com // has own authentication configuration
company2.example.com // has own (probably different) authentication
有关更多详细信息,company1 仅配置了 Facebook,company2 仅配置了 Google 身份验证。
问题:是否可以对每个主机或每个请求进行不同的身份验证?例如,一旦我知道公司,我就可以加载和使用与此请求相关的身份验证配置。
解决方案
有几种方法可以做到这一点。包括IConfiguration
在您的 facebook 和 google 计划事件中使用您的或访问 http 上下文作为服务。这是执行此操作的最干净的方法之一。您可以制作自己的方案,如下所示:
public class MyCustomAuth : AuthenticationHandler<AuthenticationSchemeOptions>
{
public const string SchemeName = "MyCustom";
public MyCustomAuth(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory
logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}
protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
{
if (Request.Host.Value == "")
{
await Context.ChallengeAsync(GoogleDefaults.AuthenticationScheme);
}
await Context.ChallengeAsync(FacebookDefaults.AuthenticationScheme);
}
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (Request.Host.Value == "")
{
return await Context.AuthenticateAsync(GoogleDefaults.AuthenticationScheme);
}
return await Context.AuthenticateAsync(FacebookDefaults.AuthenticationScheme);
}
}
您可以将所有内容添加到您的启动中并进行如下设置:
services.AddAuthentication(MyCustomAuth.SchemeName)
.AddCookie(...)
.AddFacebook(...)
.AddGoogle(...)
.AddScheme<AuthenticationSchemeOptions, MyCustomAuth>(MyCustomAuth.SchemeName, opts => { });
推荐阅读
- node.js - 我如何编写一个从 mongodb 查询的辅助函数,然后可以为任何 ejs 模板调用此函数
- responsive - 我怎样才能使网站移动友好?
- java - 在java中使用递归来反转字符串
- python - 使用 SymPy 解决具有符号数量变量的约束优化问题
- git - git push a detached head to a dev branch of remote
- php - 没有点运算符的字符串中的 PHP 函数
- node.js - 在mongodb nodejs中从具有字符串/ id数组的文档中查找数据
- c# - 使用 oledb 的 C# 更新命令
- python - 带有 groupby 和条件的 Pandas 滚动总和
- git - 列出 git 中的所有下游本地分支