.Net Core 3 OAuth2 身份验证模拟用户登录以进行集成测试

问题描述

我有一个 .Net Core 3.1 应用程序,其服务端点受 [Authorize] 保护。为了实现身份验证,我使用了 https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers 中的 OAuth 库。我正在使用 ArcGIS 提供程序,但我的问题并非针对于此;任何提供者示例都可以。我的 Startup.cs 中的安全实现如下所示:

services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = ArcGISAuthenticationDefaults.AuthenticationScheme;
    })
    .AddCookie()
    .AddArcGIS(options =>
    {
        options.ClientId = "#####";
        options.ClientSecret = "#####";
        options.AuthorizationEndpoint =
            "https://#####/arcgis/sharing/rest/oauth2/authorize";
        options.ReturnUrlParameter = "https://localhost:5001";
        options.TokenEndpoint = "https://#####/arcgis/sharing/rest/oauth2/token";
        options.UserInformationEndpoint = "https://#####/arcgis/sharing/rest/community/self";
    });

访问受保护的端点时,系统提示我进行身份验证,身份验证后工作正常。

但是,我正在尝试让我的集成测试工作——在 xUnit 测试中调用受保护的端点。我无法从测试/构建应用程序的位置访问授权服务,因此我需要模拟用户登录。

这在添加身份验证后不再起作用:

var client = _factory.CreateClient();

var response = await client.GetAsync("/protectedData/1");

我找不到关于如何使用 AspNet.Security.OAuth.Providers 库中的任何提供程序执行此操作的任何示例。任何关于如何实现这一点的帮助或示例将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...