Raghu
Raghu

Reputation: 11

Unauthorization error after installing the package Microsoft.Azure.StackExchangeRedis

I'm using Microsoft.AspNetCore.Authorization for Authorization and implemented working fine and authorize with valid token initially, after implementing Redis cache package Microsoft.Azure.StackExchangeRedis it's showing unauthorize error with valid token. For Cross verify i was removed this package and try it's Authorize perfectly. Here is my authorization code

public void ConfigureServices(IServiceCollection services)
{
  services.AddAuthentication(ServerAuthenticationDefaults.AuthenticationScheme)
             .AddIdentityServerAuthentication(options =>
             {
                 options.Authority = ServerConfig.Authority;
                 options.ApiName = ServerConfig.APIApplicationId;
                 options.RequireHttpsMetadata = false; 
                 options.JwtBackChannelHandler = new HttpClientHandler
                 {
                     ServerCertificateCustomValidationCallback = delegate { return true; }
                 };
             });
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseAuthentication();
    app.UseAuthorization();
}
using Microsoft.AspNetCore.Authorization;
 [Authorize]
 [Route("api/config")]
 public class ConfigurationController : ControllerBase
 {
 }

Here is my Redis cache code

using Azure.Identity;
using StackExchange.Redis;
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
  {
          var credential = new DefaultAzureCredential();
          string cacheConnectionEndpoint = ConfigurationUtil.GetSetting("RedisCacheConnectionString"); 
          var configurationOptions = ConfigurationOptions.Parse($"{cacheConnectionEndpoint}:6380").ConfigureForAzureWithTokenCredentialAsync(credential);
          configurationOptions.Result.AbortOnConnectFail = false;
          configurationOptions.Result.SyncTimeout = 60000;
          return ConnectionMultiplexer.Connect(configurationOptions.Result);
       });

  public static ConnectionMultiplexer Connection
  {
      get
      {
          try
          {
              return lazyConnection.Value;
          }
          catch
          {
              return null;
          }
      }
  }

WorkloadIdentityCodereferHere

Upvotes: 0

Views: 15

Answers (0)

Related Questions