.net 如何在调试过程中进行假oidc身份验证

fykwrbwg  于 2022-12-20  发布在  .NET
关注(0)|答案(2)|浏览(110)

我建一些blazor wasm项目我使用公司oidc所以在wasm我有

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";
});

我已经配置了API来使用它

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

这工作正常,但问题是如何在调试过程中跳过此登录部分所以我没有每次运行登录与我的公司帐户
我可以在API部分做,如果调试,然后允许匿名,这将为每个请求工作良好
但如何在这个前端webassembly硬编码一些'超级管理员'帐户与所有perms,使其始终使用这在调试?像假oidc?
谢谢!

jvlzgdj9

jvlzgdj91#

如果您只想创建自己的本地ClaimsPrincipal用户,那么我在Startup.cs类(请求管道)中使用了以下代码来创建/login和/logout端点:

app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/login", async context =>
    {
        var claims = new Claim[]
        {
                            //Standard claims
                            new Claim(ClaimTypes.Name, "Joe Svensson"),
                            new Claim(ClaimTypes.Country, "Sweden"),
                            new Claim(ClaimTypes.Email, "joe@edument.se"),

                            //Custom claims
                            new Claim("JobTitle", "Developer"),
                            new Claim("JobLevel", "Senior"),
        };

        ClaimsIdentity identity = new ClaimsIdentity(claims: claims,
                                          authenticationType: CookieAuthenticationDefaults.AuthenticationScheme);

        ClaimsPrincipal user = new ClaimsPrincipal(identity: identity);

        var authProperties = new AuthenticationProperties
        {
            IsPersistent = true
        };

        //Sign-in the user
        await context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user, authProperties);

        await context.Response.WriteAsync("<!DOCTYPE html><body>");
        await context.Response.WriteAsync("<h1>Logged in!</h1>");
    });

    endpoints.MapGet("/logout", async context =>
    {
        //Do add a call to the signout method here
        await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

        await context.Response.WriteAsync("<!DOCTYPE html><body>");
        await context.Response.WriteAsync("<h1>Logged out</h1>");
    });

我希望它能给你一些启发。

ruarlubt

ruarlubt2#

好的,我最终在webassembly中完成了

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

这个供应商

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));
  }

相关问题