相关疑难解决方法(0)

何时使用TryAddSingleton或AddSingleton?

我注意到在一些.net核心示例中有调用 TryAddSingleton,有些是 AddSingleton在注册服务时.

如果服务类型尚未注册,则反编译器会显示TryAdd(由TryAddSingleton调用)将指定的参数"descriptor"添加到"集合".

这是否意味着使用TryAddSingleton总是更安全,以防某些其他方法/库已经注册了同一个类?

c# dependency-injection startup asp.net-core

13
推荐指数
1
解决办法
3940
查看次数

如何配置ASP.Net TestHost与OpenId Connect一起使用?

我有一个ASP.Net核心应用程序,配置为发布和验证JWT承载令牌.当站点在Kestrel中托管时,客户端能够成功检索承载令牌并使用令牌进行身份验证.

我还有一套使用Microsoft.AspNetCore.TestHost.TestServer的集成测试.在添加身份验证之前,测试能够成功地对应用程序发出请求.添加身份验证后,我开始收到有关访问open id配置的错误.我看到的具体例外是:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://  
fail: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[3]
      Exception occurred while processing message.
System.InvalidOperationException: IDX10803: Unable to obtain configuration from: 'http://localhost/.well-known/openid-configuration'. ---> System.IO.IOException: IDX10804: Unable to retrieve document from: 'http://localhost/.well-known/openid-configuration'. ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found).
Run Code Online (Sandbox Code Playgroud)

根据我的研究,当管理局设置为与托管服务器不同的主机时,有时会触发此操作.例如,Kestrel 默认运行在http:// localhost:5000,这是我最初设置的权限,但在将其设置为TestServer正在模拟的内容(http:// localhost)时,它仍然会出现相同的错误.这是我的身份验证配置:

    app.UseJwtBearerAuthentication(new JwtBearerOptions
    {
        AutomaticAuthenticate = true,
        AutomaticChallenge = true,
        RequireHttpsMetadata = false,
        TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = signingKey,
            ValidateAudience = true …
Run Code Online (Sandbox Code Playgroud)

openid-connect asp.net-core identityserver4 asp.net-core-testhost

5
推荐指数
1
解决办法
1026
查看次数

如何在asp.net mvc核心中编写Identity Server 4的集成测试

我正在使用身份服务器4的web api.我不知道从哪里开始编写集成测试.我有一个登录控制器接收用户名和密码,用于ResourceOwnerPassword授权类型.以下是我的代码.

控制器.

[Route("Authentication/Login")]
public async Task<IActionResult> WebApiLogin(string username, string password)
{
    var accessToken = await UserAccessToken.GenerateToken(username, password);
    return new JsonResult(accessToken);
}
Run Code Online (Sandbox Code Playgroud)

用于生成令牌的类

public async Task<string> GenerateToken(string username, string password)
{
    //discover endpoint for metadata
    var disco = await DiscoveryClient.GetAsync("http://localhost:5000");

    //request token
    var clientToken = new TokenClient(disco.TokenEndpoint, "client", "secret");
    //var tokenResponse = await clientToken.RequestClientCredentialsAsync("Payment");
    var tokenResponse = await clientToken.RequestResourceOwnerPasswordAsync(username, password, "IntegrapayAPI");

    if (tokenResponse.IsError)
    {
        //Error tokenResponse.Error
        return tokenResponse.Error;
    }
    return tokenResponse.Json.ToString();
}
Run Code Online (Sandbox Code Playgroud)

IdentityServer Project启动类.

public void ConfigureServices(IServiceCollection services)
{
    services.AddIdentityServer() …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-core identityserver4

4
推荐指数
1
解决办法
2455
查看次数