小编A_d*_*per的帖子

如何限制.Net Core Web API中并发的外部API调用?

目前我正在开发一个 .net core Web api 项目,该项目从外部 Web api 获取数据。它们的末端有一个 25 的并发速率限制器(允许 25 个并发 api 调用)。第 26 个 API 调用将失败。

因此,我想在我的 Web API 项目上实现并发 API 速率限制器,并且需要跟踪失败的第 26 个 API 调用,并且需要重试(可能是 get 或 post 调用)。我的 api 代码中有多个 get 请求和 post 请求

以下是我的 Web api 中的 httpservice.cs

public HttpClient GetHttpClient()
{
    HttpClient client = new HttpClient
    {
        BaseAddress = new Uri(APIServer),
    };
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Add("Authorization", ("Bearer " + Access_Token));
    return client;
}
private HttpClient Client;
public async Task<Object> Get(string apiEndpoint)
{

    Client = …
Run Code Online (Sandbox Code Playgroud)

c# concurrency semaphore asp.net-web-api asp.net-core-webapi

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

使用 .net core Web API 验证 .net 控制台应用程序

我有一个.net core 3.1 Web API ,它是使用JWT 身份验证构建的,它与 Angular UI 集成,并且按预期工作。

以下是我的 JWT 身份验证中间件


services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})

// Adding Jwt Bearer
.AddJwtBearer(options =>
{
    options.SaveToken = true;
    options.RequireHttpsMetadata = false;
    options.IncludeErrorDetails = true;
    options.TokenValidationParameters = new TokenValidationParameters()
    {
        ValidateIssuer = false,
        ValidateAudience = false,       
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:Secret"]))    
    };
    options.Events = new JwtBearerEvents
    {
        OnAuthenticationFailed = context =>
        {
            if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
            {
                context.Response.Headers.Add("Token-Expired", "true");
            }
            return …
Run Code Online (Sandbox Code Playgroud)

c# authentication jwt asp.net-web-api asp.net-core-webapi

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