相关疑难解决方法(0)

ASP.NET Core 3.0:禁用 ASP0000 不要在“ConfigureServices”中调用“IServiceCollection.BuildServiceProvider”

我有一个 ASP.NET Core 3.0 应用程序。在 Startup 类中,在方法中

public void ConfigureServices(IServiceCollection services)
Run Code Online (Sandbox Code Playgroud)

我想配置身份验证,以便 JWT 由我的 ISecurityTokenValidator 实现处理:CustomJwtSecurityTokenHandler。为此,我需要获取实现类的实例 CustomJwtSecurityTokenHandler,其构造函数需要注入一些服务。出于这个原因,我需要打电话:

var serviceProvider = services.BuildServiceProvider();
Run Code Online (Sandbox Code Playgroud)

为了创建一个“临时” serviceProvider 来获取我的带有注入参数的 CustomJwtSecurityTokenHandler 实例。

我最终得到了这个代码(效果很好!):

services.AddAuthentication(authenticationOptions =>
{
    authenticationOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    authenticationOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(jwtBearerOptions =>
{
    jwtBearerOptions.SecurityTokenValidators.Clear();

    CustomJwtSecurityTokenHandler customJwtSecurityTokenHandler;
    {
        // create a temporary serviceProvider, in order to retrieve a CustomJwtSecurityTokenHandler (supposed to be registered as addin!).
        var serviceProvider = services.BuildServiceProvider();
        customJwtSecurityTokenHandler = serviceProvider.GetService<CustomJwtSecurityTokenHandler>();
    }

    //below line adds the custom validator class
    jwtBearerOptions.SecurityTokenValidators.Add(customJwtSecurityTokenHandler);
});
Run Code Online (Sandbox Code Playgroud)

只是,从方法 ConfigureServices 中调用 …

c# asp.net-core

10
推荐指数
0
解决办法
4713
查看次数

ASP.NET核心JWT承载令牌自定义验证

经过大量阅读,我找到了一种实现自定义JWT承载令牌验证器的方法,如下所示.

Starup.cs 代码:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
         ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseStaticFiles();

    app.UseIdentity();

    ConfigureAuth(app);

    app.UseMvcWithDefaultRoute();            
}

private void ConfigureAuth(IApplicationBuilder app)
    {

        var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.GetSection("TokenAuthentication:SecretKey").Value));


        var tokenValidationParameters = new TokenValidationParameters
        {
            // The signing key must match!
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = signingKey,
            // Validate the JWT Issuer (iss) claim
            ValidateIssuer = true,
            ValidIssuer = Configuration.GetSection("TokenAuthentication:Issuer").Value,
            // Validate the JWT Audience (aud) claim
            ValidateAudience = true,
            ValidAudience = Configuration.GetSection("TokenAuthentication:Audience").Value,
            // Validate the token expiry …
Run Code Online (Sandbox Code Playgroud)

c# asp.net jwt asp.net-core asp.net-core-middleware

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

使用AspNet Core 2.0进行Google JWT身份验证

我试图在我的ASP.NET Core 2.0 web api中集成google身份验证,我无法弄清楚如何让它工作.

我在我的Startup.cs中有这个代码ConfigureServices:

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultTokenProviders();

services.AddAuthentication()
.AddGoogle(googleOptions => 
 {
     googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
     googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
});
Run Code Online (Sandbox Code Playgroud)

这个在Configure(IApplicationBuilder app, IHostingEnvironment env):

 app.UseAuthentication();
Run Code Online (Sandbox Code Playgroud)

当我导航到Authorized端点时,结果是302 Found因为它可能是重定向到某个登录端点(我从未创建过).如何阻止重定向,只是让API期望一个令牌,401如果没有提供令牌,则返回一个令牌?

google-api google-authentication oauth-2.0 asp.net-core asp.net-core-2.0

5
推荐指数
2
解决办法
5314
查看次数