question

DikongPrigm-9942 avatar image
0 Votes"
DikongPrigm-9942 asked DikongPrigm-9942 edited

JWT Token Error in AZURE

ERROR MESSAGE RECEIVED

 ArgumentNullException: String reference not set to an instance of a String. (Parameter 's')
 System.Text.Encoding.GetBytes(string s)
 System.Text.UTF8Encoding+UTF8EncodingSealed.GetBytes(string s)
 API.Extensions.IdentityServiceExtensions+<>c__DisplayClass0_0.<AddIdentityServices>b__0(JwtBearerOptions options) in IdentityServiceExtensions.cs
 Microsoft.Extensions.Options.ConfigureNamedOptions<TOptions>.Configure(string name, TOptions options)
 Microsoft.Extensions.Options.OptionsFactory<TOptions>.Create(string name)
 Microsoft.Extensions.Options.OptionsMonitor<TOptions>+<>c__DisplayClass11_0.<Get>b__0()
 System.Lazy<T>.ViaFactory(LazyThreadSafetyMode mode)
 System.Lazy<T>.ExecutionAndPublication(LazyHelper executionAndPublication, bool useDefaultConstructor)
 System.Lazy<T>.CreateValue()
 System.Lazy<T>.get_Value()
 Microsoft.Extensions.Options.OptionsCache<TOptions>.GetOrAdd(string name, Func<TOptions> createOptions)
 Microsoft.Extensions.Options.OptionsMonitor<TOptions>.Get(string name)
 Microsoft.AspNetCore.Authentication.AuthenticationHandler<TOptions>.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
 Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, string authenticationScheme)
 Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, string scheme)
 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
 DevExpress.AspNetCore.Internal.BinaryStorageMiddleware.Invoke(HttpContext httpContext)
 DevExpress.AspNetCore.Internal.ResourceManagerMiddleware.Invoke(HttpContext httpContext)
 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)


This error is coming from this class

 public static class IdentityServiceExtensions
     {
         public static IServiceCollection AddIdentityServices(this IServiceCollection services,
             IConfiguration config)
         {
             var builder = services.AddIdentityCore<AppUser>();
    
             builder = new IdentityBuilder(builder.UserType, builder.Services);
             builder.AddEntityFrameworkStores<MultiChaxContext>();
             builder.AddSignInManager<SignInManager<AppUser>>();
    
             services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                 .AddJwtBearer(options =>
                 {
                     options.TokenValidationParameters = new TokenValidationParameters
                     {
                         ValidateIssuerSigningKey = true,
                         IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["Token:Key"])),
                         ValidIssuer = config["Token:Issuer"],
                         ValidateIssuer = true,
                         ValidateAudience = false
                     };
                 });
    
             return services;
         }
     }



Just by reading the exception message, I can tell that the TOKEN KEY is null but here's my appSetting.json for your reference

 {
   "Logging": {
     "LogLevel": {
       "Default": "Warning"
     }
   },
   "ConnectionStrings": {
     "DefaultConnection": "My connection string here"
   },
   "Token": {
     "Key": "super secret key",
     "Issuer": "https://localhost:5001/"
   },
   "AllowedHosts": "*"
 }

As you can see, there is a token key in the json file.
To add to that, the website works locally but after being deployed in azure, it doesn't and i received the error above.

Thanks for the help.

dotnet-aspnet-core-security
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers