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

标签 asp.net authentication asp.net-web-api owin katana

我刚刚开始将 OWIN\Katana 用于 Web API 项目。它使用 Windows 身份验证。这似乎有效,但我的大多数集成测试都失败了。他们之前只使用内存中 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 Hook ,稍后我们将看到它仅在集成测试中使用:

    // 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. 在集成测试的设置代码中,我注入(inject)了测试授权过滤器:

    Startup.CreateAuthFilter = () => new TestAuthFilter();
    var TestServer = TestServer.Create<Startup>();
    
  4. 当特定测试需要时,我将 TestUserId 设置为已知值,而其他测试似乎可以工作,因为存在身份验证过滤器:

    TestAuthFilter.TestUserId = testUser.UserId;
    

我在这里分享这个是为了帮助其他人,但请有人告诉我更好的方法!至少我确信有一种更好的方法来注入(inject)我的测试过滤器,而无需在启动中包含代码...我只是没有想到。

关于asp.net - 使用 Microsoft.Owin.Testing.TestServer 进行身份验证以进行内存中集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897705/

相关文章:

c# - 处置 Web 服务代理类?

asp.net - Google Chrome 的消息 "A data breach on a site or app exposed your password"是否表明我的网页存在漏洞?

java - 登录表单 IF-ELSE 无法正常工作

flutter - sms_otp_auto_verify 无法自动检测 OTP

android - 在 Android 中调用 .NET Web API Post Service 发送空消息

c# - 使用 HttpListener 的 Windows 桌面应用程序的 OAuth 2.0 授权

css - 关闭兼容性 View IE11 时链接按钮未触发的问题

c# - 如何验证IList外键? Entity Framework

asp.net - Plon 和 Asp.Net 集成

c# - 当你的 MVC/Web API 不是从上到下异步时会发生什么?