使用Microsoft.Owin.Testing.TestServer进行身份验证以进行内存中集成测试

互斥体

我刚开始将OWIN \ Katana用于Web api项目。它使用Windows身份验证。这似乎可行,但是我的大多数集成测试都已失败。他们以前只是在使用In-Memory,HttpServer但我已改为使用Microsoft.Owin.Testing.TestServer我已经在测试设置中替换了以下内容:

        var config = new HttpConfiguration { IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always };
        config.EnableQuerySupport();
        Server = new HttpServer(config);
        MyConfigClass.Configure(config);
        WebApiConfig.Register(config);

用一个更简单的方法:

TestServer = TestServer.Create<Startup>();

但是,以前我可以使用以下方法对内存服务器进行“伪造”身份验证:

Thread.CurrentPrincipal = new ClientRolePrincipal(new HttpListenerBasicIdentity(Username, Password));

现在这不起作用。对于所有请求,我都会收到以下信息:

System.Exception : {"Message":"Authorization has been denied for this request."}

如何使用内存中的OWIN测试服务器进行身份验证或至少绕过身份验证?

互斥体

我已经能够以某种肯定最佳的方式来解决此问题,但是必须等到遇到更好的解决方案,或者你们中的一个很好的人告诉我一种更好的方法时才可以这样做:)我已完成,如下所示:

  1. 在我的Startup类中,我添加了一个CreateAuthFilter钩子,稍后我们将看到它仅在集成测试中使用:

    // Sample Startup class
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();
    
            // Use CreateFilter Method to create Authorisation Filter -  if not null add it
            var authFilter = CreateAuthFilter();
            if(authFilter != null)
                config.Filters.Add(authFilter);
    
            // Other configuration and middleware...
        }
    
        public static Func<IFilter> CreateAuthFilter = () => null;
    }
    
  2. 实现了仅在集成测试中使用的授权过滤器:

    public class TestAuthFilter : IAuthenticationFilter
    {
        static TestAuthFilter()
        {
            TestUserId = "TestDomain\\TestUser";
        }
    
        public bool AllowMultiple { get; private set; }
    
        public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
        {
            context.Principal = new ClientRolePrincipal(new HttpListenerBasicIdentity(TestUserId, "password")); ;
        }
    
        public static string TestUserId { get; set; }
    
        public async Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
        {
    
        }
    }
    
  3. 在用于集成测试的设置代码中,注入测试授权过滤器:

    Startup.CreateAuthFilter = () => new TestAuthFilter();
    var TestServer = TestServer.Create<Startup>();
    
  4. 在特定测试中需要时,我将TestUserId设置为已知值,并且其他测试似乎也可以正常工作,因为存在Auth过滤器:

    TestAuthFilter.TestUserId = testUser.UserId;
    

我在这里分享这是为了帮助别人,但是请有人告诉我更好的方法!至少我敢肯定,有一种更好的方法可以在不将代码包含在启动中的情况下注入测试过滤器……我只是没有想到这一点。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用OWIN身份验证的CORS无法在Web API中使用

如何使用OWIN OAuthBearerAuthentication验证访问令牌?

具有Azure Active Directory身份验证票证寿命的Microsoft.Owin.Security.OpenIdConnect

找不到Microsoft.Owin.Testing.TestServer 404

在Osp Mvc上使用OWIN进行自定义身份验证

使用OWIN进行基于区域的身份验证

使用OWIN / OIDC针对Azure AD B2C对自定义Umbraco控制器进行身份验证

使用Powershell对Microsoft Graph API进行身份验证

如何使用Passport在phpunit Testing中对用户进行身份验证

无法使用Web API OWIN JWT令牌对桌面APP进行身份验证

使用OWIN JWT承载身份验证来验证RS256签名的JWT令牌

Owin的SAML2-无法进行身份验证

更改Microsoft.Owin.Testing.TestServer的HttpClient上的超时无效

Web API-OWIN中HttpContext.Current的问题,用于使用moq进行集成测试

为使用IdentityServer 4进行身份验证的AspNetCore API构建集成测试

协商身份验证和Microsoft.AspNetCore.Mvc.Testing

硒测试:使用Webauthn进行身份验证

使用OWIN SelfHost和Facebook身份验证

使用Spring Security内存身份验证进行内部化

如何使用自定义身份验证和内存托管进行ASP.NET Web API集成测试

尽管会话丢失,但Owin仍对用户进行身份验证

使用Capybara / Minitest处理身份验证以进行集成测试

Microsoft.Owin.Testing.TestServer不能与Fiddler一起使用

OWIN Cookie身份验证

通过Google进行OWIN身份验证

在Owin上使用JWT与RSA进行身份验证

Auth0 - 在 Owin 上使用带有承载访问令牌的 JWT 使用 RS256 进行身份验证

使用 Windows 集成身份验证对自定义 api 进行身份验证

使用身份验证在 Django 中进行测试