【问题标题】:How do I set AuthenticationScheme and AutomaticAuthenticate in .NET Core 2?如何在 .NET Core 2 中设置 AuthenticationScheme 和 AutomaticAuthenticate?
【发布时间】:2017-09-14 08:29:26
【问题描述】:

我尝试按照本指南在 .NET core 2 中运行测试时允许用户登录

http://geeklearning.io/how-to-deal-with-identity-when-testing-an-asp-net-core-application/

它说我应该为这样的测试目的配置身份验证中间件:

public class TestAuthenticationOptions : AuthenticationOptions  
{
    public virtual ClaimsIdentity Identity { get; } = new ClaimsIdentity(new Claim[]
    {
        new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", Guid.NewGuid().ToString()),
        new Claim("http://schemas.microsoft.com/identity/claims/tenantid", "test"),
        new Claim("http://schemas.microsoft.com/identity/claims/objectidentifier", Guid.NewGuid().ToString()),
        new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname", "test"),
        new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname", "test"),
        new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn", "test"),
    }, "test");

    public TestAuthenticationOptions()
    {
        this.AuthenticationScheme = "TestAuthenticationMiddleware";
        this.AutomaticAuthenticate = true;
    }
}

这不起作用,因为 AuthenticationOptions 类已从 .NET Core 2 中的 AuthenticationOptions 实例中删除了 AuthenticationSchemeAutomaticAuthenticate 属性,参见。 https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x#setting-default-authentication-schemes

所以现在我不知道如何登录测试以在 .NET Core 2 中工作。

【问题讨论】:

    标签: c# asp.net testing .net-core


    【解决方案1】:

    您可以在您的 startup.cs 中设置配置选项

    services.AddAuthentication(config => {
        this.AuthenticationScheme = "TestAuthenticationMiddleware";
        this.AutomaticAuthenticate = true;
    });
    

    我在 .net core 2.0 中找到的大多数代码都使用这种方式来配置 AuthenticationOptions。但是,如果您需要创建自己的 AuthenticationOptions 类并使用。您可以创建自己的方案,这意味着您还需要创建一个AuthenticationHandler<TestAuthenticationOptions> 类(我们称之为TestAuthenticationHandler)并将其添加为方案。

    services.AddAuthentication(config => {
        this.AuthenticationScheme = "TestAuthenticationMiddleware";
        this.AutomaticAuthenticate = true;
    })
    .AddScheme<TestAuthenticationHandler, TestAuthenticationOptions>("TestAuthenticationMiddleware", o => {});
    

    【讨论】:

    • 我收到此错误消息:AuthenticationOptions 不包含“AutomaticAuthenticate”的定义,并且当我输入此代码时,找不到接受“AuthenticationOptions”类型的第一个参数的扩展方法“AutomaticAuthenticate”: services.AddAuthentication(options => { options.AuthenticationScheme = "TestAuthenticationMiddleware"; options.AutomaticAuthenticate = true; });
    • 当我输入您的建议时:“Startup”不包含“AuthenticationScheme”的定义,并且找不到接受“Startup”类型的第一个参数的扩展方法“AuthenticationScheme”跨度>
    • 好吧,似乎一旦您将AutomaticAuthenticate 设置为true,就需要进行更多设置。我不完全确定AutomaticAuthenticate 的工作原理,我刚刚从您的代码中复制了这些值。您尝试了第一个还是第二个代码?
    【解决方案2】:

    你不能再那样做了。 AuthenticationScheme 只能在 Startup.cs 中设置。像你这样的分支不再可能了。 您应该使用应用程序中使用的 Scheme,而不是 test-Scheme。

    public class TestAuthenticationOptions : AuthenticationOptions  
    {
    public virtual ClaimsIdentity Identity { get; } = new ClaimsIdentity(new Claim[]
    {
        new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", Guid.NewGuid().ToString()),
        new Claim("http://schemas.microsoft.com/identity/claims/tenantid", "test"),
        new Claim("http://schemas.microsoft.com/identity/claims/objectidentifier", Guid.NewGuid().ToString()),
        new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname", "test"),
        new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname", "test"),
        new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn", "test"),
    }, "<ProductionScheme>");
    
        public TestAuthenticationOptions
        {
             //nothing to do here.
        }
    }
    

    希望在 Startup 中:

    services.AddAuthentication("<ProductionScheme>");
    

    也许这可以帮助你:

    https://github.com/aspnet/Announcements/issues/262

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-15
      • 2018-06-19
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多