.净核绕过或关闭[授权(角色="")]在地方发展

0

的问题

我已经下码绕过增加认证在地方发展,我是使用广告.净的核心。

#if !DEBUG
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
               .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd"));
#endif

然而,因为我有我的控制器的保护授权的属性,我怎么绕过授权的属性内部控制器在地方发展:

[Authorize(Roles = "Buyer")]
public class ProductController : ApiBaseController
{
}

中。净框架,我有如下代码复盖的授权属性:

public class MyAuthorizeAttribute : AuthorizeAttribute
    {
     #if DEBUG
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            return true;
        }
     #endif
    }

什么是等效的代码。净核心? 或是有任何其他方式,我们可以复盖授权的属性在启动。cs类?

3
2

我认为你可以使用 IClaimsTransformation 为。 在这种情况下,我只是添加一个角色给大家,但是当它连线,它只会做到这一点,如果你是在开发(注:需要确保环境的可变设置是否正确如此 IsDevelopment 作品)。

AddRolesClaimsTransformation.cs
/// <summary>
/// Adds roles to a user on the fly, this will hard code a Buyer role for everyone.
/// </summary>
public class AddRolesClaimsTransformation : IClaimsTransformation
{
    public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
    {
        // Clone current identity
        var clone = principal.Clone();
        var ident = (ClaimsIdentity)clone.Identity;

        ident.AddClaim(new Claim(ClaimTypes.Role, "Buyer"));

        return clone;
    }
}
启动。cs
// Only in dev
if (builder.Environment.IsDevelopment())
{
    builder.Services.AddScoped<IClaimsTransformation, AddRolesClaimsTransformation>();
}

这应该工作ASP.NET 核心3.1根据Microsoft文档。 我测试了它反对。净6然而(。净6的模板,为新的网站移动 Startup.cs 东西到 Program.cs).

其他一个侧面说明,如果你依靠的 IsDevelopment 管道上 WebHostEnvironment 你不会有使用编译器的指令。 这样,一旦环境是建立它只会的工作,但是部署有(例如没有一个机会的一个意外调试建立将使它成为一个环境,它不应该是中)。

2021-11-23 23:24:51
2

而不是明确指定的角色,每个控制器的要求 [Authorize(Roles...你可以使用 [Authorize(Policy... 添加一个间接层.

这样你可以决定在你 StartUp 类(或 IConfigureOptions<AuthorizationOptions> 服务),究竟什么每个政策手段。 包括任何其他奇怪的要求。

[Authorize(Policy= "Buyer")]
public class ProductController : ApiBaseController

...

services.AddAuthorization(o =>
{
    o.AddPolicy("Buyer", b => {
#if DEBUG
        b.RequireAuthenticatedUser();
#else
        b.RequireRole("Buyer");
#endif
    });
});
2021-11-23 23:18:53

非易失存储器我得到了它,谢谢你
VR1256
0

我得到了它的工作用以下代码感谢杰里米*为正确的方向:

在控制器I类使用的政策基础的授权:

 [Authorize(Policy= "Buyer")]
 public class ProductController : ApiBaseController
 {
 }

在开始。cs我们可以添加的认证和授权的基础上调试条件:

#if !DEBUG
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
               .AddMicrosoftIdentityWebApi(config.GetSection("AzureAd"));
#endif

            services.AddAuthorization(options =>
            {
                // policy for read access
                options.AddPolicy("Buyer", policy =>
                {
#if DEBUG
                    policy.RequireAuthenticatedUser();
#else
                    policy.RequireRole("Buyer");
#endif
                });
            });

为RequireAuthenticatedUser()在调试模式中,我们使用以下代码添加AllowAnonymous属性的所有控制器:

app.UseEndpoints(endpoints =>
            {
#if DEBUG
                endpoints.MapControllers().WithMetadata(new AllowAnonymousAttribute());
#else
                    endpoints.MapControllers();
#endif
            });
2021-11-24 00:59:47

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................