C# 无法对客户端进行身份验证,错误:IDX20804:无法从以下位置检索文档:';[PII隐藏]';

C# 无法对客户端进行身份验证,错误:IDX20804:无法从以下位置检索文档:';[PII隐藏]';,c#,unit-testing,.net-core,asp.net-identity,azure-ad-b2c,C#,Unit Testing,.net Core,Asp.net Identity,Azure Ad B2c,我在使用Azure AD B2C(承载令牌)的.Net Core API时遇到了一个问题。该应用程序运行良好,但我还想编写单元测试,例如简单GET。我在运行测试时遇到以下错误: System.Net.Http.HttpRequestException : Error while copying content to a stream. ---- System.IO.IOException : -------- System.InvalidOperationException : IDX2080

我在使用Azure AD B2C(承载令牌)的.Net Core API时遇到了一个问题。该应用程序运行良好,但我还想编写单元测试,例如简单GET。我在运行测试时遇到以下错误:

System.Net.Http.HttpRequestException : Error while copying content to a stream.
---- System.IO.IOException : 
-------- System.InvalidOperationException : IDX20803: Unable to obtain configuration from: '[PII is hidden by default. Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]'.
------------ System.IO.IOException : IDX20804: Unable to retrieve document from: '[PII is hidden by default. Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]'.
---------------- System.Net.Http.HttpRequestException : Response status code does not indicate success: 404 (Not Found).
   at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at Ttpsc.DotNetInternship.SandwichApp.Tests.InMemory.Base.BaseTest.Get[T](String endpoint, T parameter) in C:\...SandwichApp.Tests\InMemory\Base\BaseTest.cs:line 90
   at C:\..SandwichApp.Tests.ControllersTest.GroupsControllerTest.GetGroups_GetAllGroups() in C:\..SandwichApp.Tests\ControllersTest\GroupsControllerTest.cs:line 20
--- End of stack trace from previous location where exception was thrown ---
----- Inner Stack Trace -----
   at Microsoft.AspNetCore.TestHost.ResponseStream.CheckAborted()
   at Microsoft.AspNetCore.TestHost.ResponseStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
   at System.IO.Stream.CopyToAsyncInternal(Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
   at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
----- Inner Stack Trace -----
   at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
   at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
   at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
   at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync()
   at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.TestHost.HttpContextBuilder.<>c__DisplayClass10_0.<<SendAsync>b__0>d.MoveNext()
----- Inner Stack Trace -----
   at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel)
   at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel)
   at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
----- Inner Stack Trace -----
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel)

听起来好像它正在尝试获取元数据JSON文档。如果要在不使用auth的情况下运行测试,可以将测试配置为在未配置auth的环境中运行。否则,您必须正确配置它,以便它能够获取元数据;(请记住在将代码发布到生产环境之前删除此选项)
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Flurl.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Newtonsoft.Json.Linq;

namespace Ttpsc.DotNetInternship.SandwichApp.Tests.InMemory.Base
{
    public abstract class BaseTest : IDisposable
    {
        private bool _disposed;

        protected BaseTest()
        {
            Server = new TestServer(new WebHostBuilder()
                .UseStartup<Startup>());

            Services = Server.Host.Services;


            Client = Server.CreateClient();
            //Client.DefaultRequestHeaders.Accept.Clear();

            Client.DefaultRequestHeaders
                .Accept
                .Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        protected TestServer Server { get; set; }

        protected HttpClient Client { get; set; }

        protected IServiceProvider Services { get; set; }


        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }




        protected Task<HttpResponseMessage> Get(string endpoint)
        {
            return Get<string>(endpoint, null);
        }

        protected async Task<HttpResponseMessage> Get<T>(string endpoint, T parameter)
        {
            var tt = await GetToken();

            var jo = JObject.Parse(tt);
            var token = jo["access_token"].ToString();


            Client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);



            var url = string.Concat(endpoint, parameterQuery);
            Console.WriteLine(url);


            return await Client.GetAsync(url);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
                if (disposing)
                {
                    Client?.Dispose();
                    Server?.Dispose();
                }

            _disposed = true;
        }
    }
}
using System.Net;
using System.Threading.Tasks;
using Ttpsc.DotNetInternship.SandwichApp.Tests.InMemory.Base;
using Xunit;

namespace Ttpsc.DotNetInternship.SandwichApp.Tests.ControllersTest
{
    public class GroupsControllerTest : BaseTest
    {
        [Fact]
        public async Task GetGroups_GetAllGroups()
        {
            // Arrange

            var url = "api/groups";
            var expected = HttpStatusCode.OK;

            // Act

            var response = await Get(url);

            // Assert
            Assert.Equal(expected, response.StatusCode);
        }
    }
}