Search code examples
.netauthenticationopenid-connectblazor-webassemblyasp.net-blazor

how to do fake oidc auth during debug


im building some blazor wasm project i use company oidc so in wasm i have

 builder.Services.AddOidcAuthentication(opt =>
{
  opt.ProviderOptions.Authority = "https://xx.zz.pl/auth/cp";
  opt.ProviderOptions.ClientId = "xxx";
  opt.ProviderOptions.DefaultScopes.Add("email");
  opt.ProviderOptions.ResponseType = "code";
});

and i have api configured to use this

 builder.Services
   .AddAuthentication("Bearer")
   .AddJwtBearer("Bearer", options =>
   {
      options.Authority = "https://xx.zz.pl/auth/cp";;
   });
 

and this works fine but question is how to skip this logon part during debug so i do not have everytime i run login with my corp account

i can do on api part do that if debug then allow anonym and this will work fine for every request

but how in this frontend webassembly to hardcode some 'superadmin' account with all perms so it use this always during debug ? like fake oidc?

thanks and regards !


Solution

  • ok so i did eventually in webassembly

    if (builder.HostEnvironment.IsDevelopment()) 
    {
       builder.Services.AddScoped<AuthenticationStateProvider, 
       DebugAuthStateProvicer>();
    }
    else 
     builder.Services.AddOidcAuthentication......
    

    and this provider like

      public class DebugAuthStateProvicer : AuthenticationStateProvider
      {
        public override async Task<AuthenticationState> GetAuthenticationStateAsync()
        {
    
                var identity = new ClaimsIdentity();
                var claims = new List<Claim> { new Claim(ClaimTypes.Name, "DebugAdmin") };
    
                claims.Add(new Claim(ClaimTypes.Role, "Admin"));
    
                identity = new ClaimsIdentity(claims, "Server authentication");
        }
       
        return new AuthenticationState(new ClaimsPrincipal(identity));
      }