小编Max*_*xim的帖子

Asp.Net Core:注册实现多个接口和生活方式Singleton

考虑以下接口和类定义:

public interface IInterface1 { }
public interface IInterface2 { }
public class MyClass : IInterface1, IInterface2 { }
Run Code Online (Sandbox Code Playgroud)

有没有办法注册MyClass多个接口的一个实例,如下所示:

...
services.AddSingleton<IInterface1, IInterface2, MyClass>();
...
Run Code Online (Sandbox Code Playgroud)

并MyClass使用不同的接口解决此单个实例,如下所示:

IInterface1 interface1 = app.ApplicationServices.GetService<IInterface1>();
IInterface2 interface2 = app.ApplicationServices.GetService<IInterface2>();
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection inversion-of-control asp.net-core

41
推荐指数
3
解决办法
1万
查看次数

ASP.NET核心中的承载令牌认证

尝试在简单的.Net Core Web API项目中使用基于承载令牌的身份验证.这是我的Startup.cs

app.UseMvc();
//---
const string secretKey = "mysupersecret_secretkey!123";
SymmetricSecurityKey signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
SigningCredentials signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
//---
const string audience = "Audience";
const string issuer = "Issuer";
//---
TokenValidationParameters tokenValidationParameters = new TokenValidationParameters
{
    ValidateIssuerSigningKey = true,
    IssuerSigningKey = signingKey,

    ValidateIssuer = false,
    ValidIssuer = issuer,

    ValidateAudience = true,
    ValidAudience = audience,

    ValidateLifetime = true,

    ClockSkew = TimeSpan.Zero,
    AuthenticationType = JwtBearerDefaults.AuthenticationScheme
};
//---
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    TokenValidationParameters …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core

14
推荐指数
2
解决办法
1万
查看次数