简体   繁体   English

在 Startup.cs 之外更新 asp.net 核心身份验证

[英]Update asp.net core Authentication outside of Startup.cs

With respect to asp.net core identity management, we have a requirement to change the Microsoft ClientId and ClientSecret after our asp.net core app has started and, therefore, not in startup.cs.关于asp.net 核心身份管理,我们需要在asp.net 核心应用程序启动后更改Microsoft ClientId 和ClientSecret,因此不在startup.cs 中。 We have various identity management logins working fine with, for example this for Microsoft Azure:我们有各种身份管理登录可以正常工作,例如 Microsoft Azure:

            .AddMicrosoftAccount(microsoftOptions =>
            {
                microsoftOptions.CorrelationCookie.HttpOnly = true;
                microsoftOptions.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
                microsoftOptions.ClientId = "removed";
                microsoftOptions.ClientSecret = "removed";
            })

We now need to change the ClientId and ClientSecret dynamically after the core application has started and what we can't figure out is how to access this from the services collection later in other pages so we can update them.我们现在需要在核心应用程序启动后动态更改 ClientId 和 ClientSecret,我们无法弄清楚如何稍后在其他页面中从服务集合访问它,以便我们可以更新它们。

Any help appreciated.任何帮助表示赞赏。

Thanks.谢谢。

ASP.NET Core provides IAuthenticationSchemeProvider interface to dynamically add/remove authentication schemes at runtime. ASP.NET Core 提供IAuthenticationSchemeProvider接口以在运行时动态添加/删除身份验证方案。 You can inject this interface and add Microsoft Account auth schemes after the app has started.您可以在应用程序启动后注入此接口并添加Microsoft 帐户身份验证方案。

Using Microsoft's demo app as reference, here's a basic implementation:使用Microsoft 的演示应用程序作为参考,这是一个基本实现:

public class DynamicAuthController: ControllerBase
{
    private IAuthenticationSchemeProvider _schemeProvider;
    private IOptionsMonitorCache<MicrosoftAccountOptions> _optionsCache;

    public DynamicAuthController(IAuthenticationSchemeProvider schemeProvider, IOptionsMonitorCache<MicrosoftAccountOptions> optionsCache)
    {
        _schemeProvider = schemeProvider;
        _optionsCache = optionsCache;
    }

    [HttpPost]
    public ActionResult Add()
    {
        var schemeName = "MicrosoftCustom1"; // must be unique for different schemes
        var schemeOptions = new MicrosoftAccountOptions
        {
            ClientId = "ididid", // fetch credentials from another service or database
            ClientSecret = "secretsecret",
            CorrelationCookie =
            {
                HttpOnly = true,
                SecurePolicy = CookieSecurePolicy.Always
            }
        };
        
        var scheme = new AuthenticationScheme(schemeName, displayName:null, typeof(MicrosoftAccountHandler));
        _schemeProvider.TryAddScheme(scheme);
        _optionsCache.TryAdd(
            schemeName,
            schemeOptions
        );

        return Ok();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM