Is there a way to add claims in an ASP.NET Core middleware after Authentication?

后端 未结 5 1630
梦谈多话
梦谈多话 2021-02-04 02:30

I have this in my startup:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperE         


        
5条回答
  •  孤城傲影
    2021-02-04 03:20

    You can add another middleware immediately after the UseAuthentication() to add claims :

    app.UseAuthentication();
    app.Use(async(context, next)=>{
        if(context.User !=null && context.User.Identity.IsAuthenticated){
            // add claims here 
            context.User.Claims.Append(new Claim("type-x","value-x"));
        }
        await next();
    });
    
    //  call other middlewares 
    app.UseMiddleware();
    

提交回复
热议问题