Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Identity Server 4-API在部署到服务器时返回401未经授权_C#_Api_Identityserver4_Blazor_Asp.net Core 3.1 - Fatal编程技术网

C# Identity Server 4-API在部署到服务器时返回401未经授权

C# Identity Server 4-API在部署到服务器时返回401未经授权,c#,api,identityserver4,blazor,asp.net-core-3.1,C#,Api,Identityserver4,Blazor,Asp.net Core 3.1,我正在使用Blazor服务器应用程序,通过Identity Server 4进行身份验证,并访问asp.net core 3.1 API。在本地,我在端口5000上安装了Identity Server,在5001上安装了API,在5002上安装了Blazor应用程序 在我们的测试服务器上,我们在IIS中托管了所有三个(一个服务器上是Blazor,另一个服务器上是API和Identity server)。当访问带有授权标签的控制器时,我得到401未经授权。我可以登录,我知道API仍然可以访问,因为

我正在使用Blazor服务器应用程序,通过Identity Server 4进行身份验证,并访问asp.net core 3.1 API。在本地,我在端口5000上安装了Identity Server,在5001上安装了API,在5002上安装了Blazor应用程序

在我们的测试服务器上,我们在IIS中托管了所有三个(一个服务器上是Blazor,另一个服务器上是API和Identity server)。当访问带有授权标签的控制器时,我得到401未经授权。我可以登录,我知道API仍然可以访问,因为不是所有的控制器都需要授权,并且数据都可以通过。此外,我知道有一个令牌,因为我曾经将它打印到页面上

在我们的测试环境中,API不再接受Identity Server生成的令牌的原因可能是什么

以下是我的代码示例,如果需要,我将很乐意添加更多:

授权控制人

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("api/[controller]")]
[ApiController]
public class Controller : ControllerBase
API资源

public static IEnumerable<ApiResource> Apis =>
  new ApiResource[]
  {
    new ApiResource("Api", "MyApi")

  };
客户端配置

services.AddAuthentication("Bearer")
 .AddJwtBearer("Bearer", options =>
 {
     options.Authority = AuthUrl; // IS loc

     options.Audience = "Api"; 
 });
services.AddSingleton<IDiscoveryCache>(sp =>
            {
                var factory = sp.GetRequiredService<IHttpClientFactory>();
                return new DiscoveryCache(
                    AuthUrl,
                    () => factory.CreateClient());
            });
            services.AddAuthentication(options =>
            {
                options.DefaultScheme = "Cookies";
                options.DefaultChallengeScheme = "oidc";
            })
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
            {
                options.Authority = AuthUrl;
                options.ClientId = ClientID;
                options.ClientSecret = ClientSecret;
                options.ResponseType = "code id_token";
                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;

                // part 3
                options.Scope.Add("openid");
                options.Scope.Add("offline_access");
                options.Scope.Add("Api");
                options.RequireHttpsMetadata = false;
            });

我使用了通过openssl生成的自签名证书,并且在开发人员环境中通过不使用.developerssigningcredentials对其进行了测试。

确保您网站的IIS池可以访问您的私人证书密钥

您可以尝试使用LocalSystem

确保您网站的IIS池可以访问您的私人证书密钥

您可以尝试使用LocalSystem

api配置中的受众价值对我来说非常突出。它应该是所使用的客户端的客户端id。另外,您可以在示例令牌中发布AuthUrl的值和一些声明吗?显示。访问群体是指API资源名称/id吗?在我的机器上,AuthUrl是“Server.Address:5000”或localhost:5000。您是否尝试过邮递员发出请求?类似的结果。401未经授权,使用Identity Server授予的令牌。对我来说,最突出的一点是api配置中的受众价值。它应该是所使用的客户端的客户端id。另外,您可以在示例令牌中发布AuthUrl的值和一些声明吗?显示。访问群体是指API资源名称/id吗?在我的机器上,AuthUrl是“Server.Address:5000”或localhost:5000。您是否尝试过邮递员发出请求?类似的结果。401未经授权,使用Identity Server授予的令牌。
services.AddSingleton<IDiscoveryCache>(sp =>
            {
                var factory = sp.GetRequiredService<IHttpClientFactory>();
                return new DiscoveryCache(
                    AuthUrl,
                    () => factory.CreateClient());
            });
            services.AddAuthentication(options =>
            {
                options.DefaultScheme = "Cookies";
                options.DefaultChallengeScheme = "oidc";
            })
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
            {
                options.Authority = AuthUrl;
                options.ClientId = ClientID;
                options.ClientSecret = ClientSecret;
                options.ResponseType = "code id_token";
                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;

                // part 3
                options.Scope.Add("openid");
                options.Scope.Add("offline_access");
                options.Scope.Add("Api");
                options.RequireHttpsMetadata = false;
            });
// not recommended for production - you need to store your key material somewhere secure
        if (Environment.IsDevelopment())
        {
            builder.AddDeveloperSigningCredential();
        }
        else
        {
            X509Certificate2 cert = null;
            using (var certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine))
            {
                certStore.Open(OpenFlags.ReadOnly);
                var certCollection = certStore.Certificates.Find(
                    X509FindType.FindByThumbprint,
                    "76588d12094bc26991faf9e3eaaa241d973fe72f", // Change this with the thumbprint of your certificate
                    false);

                if (certCollection.Count > 0)
                {
                    cert = certCollection[0];
                }
            }
            builder.AddSigningCredential(cert);
        }