绕过.Net Core中的授权属性以获取发行版本
有没有办法在asp.net核心中“绕过”授权? 我注意到Authorize属性不再具有AuthorizeCore方法,您可以使用该方法来决定是否继续使用auth。
Pre .net核心你可以这样做:
protected override bool AuthorizeCore(HttpContextBase httpContext) { // no auth in debug mode please #if DEBUG return true; #endif return base.AuthorizeCore(httpContext); }
我希望我不会错过任何明显的显而易见的东西,但如果需要的话,能够在DEBUG中跳过auth工作流程会更好。 我只是无法找到.net核心
正如评论中所指出的,您可以为所有需求处理程序创建基类。
public abstract class RequirementHandlerBase : AuthorizationHandler where T : IAuthorizationRequirement { protected sealed override Task HandleRequirementAsync(AuthorizationHandlerContext context, T requirement) { #if DEBUG context.Succeed(requirement); return Task.FromResult(true); #else return HandleAsync(context, requirement); #endif } protected abstract Task HandleAsync(AuthorizationHandlerContext context, T requirement); }
然后从此基类派生您的需求处理程序。
public class AgeRequirementHandler : RequirementHandlerBase { protected override HandleAsync(AuthorizationHandlerContext context, AgeRequirement requirement) { ... } } public class AgeRequirement : IRequrement { public int MinimumAge { get; set; } }
然后注册它。
services.AddAuthorization(options => { options.AddPolicy("Over18", policy => policy.Requirements.Add(new AgeRequirement { MinimumAge = 18 })); });
我想到了两种可能的解决方案。
首先是使用伪Authentication Middleware
。 您可以创建这样的伪身份validation中间件。 你的Startup.cs
应该是这样的(你应该照顾假服务):
private IHostingEnvironment _env; public Startup(IHostingEnvironment env) { _env = env; // other stuff } public void ConfigureServices(IServiceCollection services) { // ... if (_env.IsDevelopment()) { // dev stuff services.AddTransient(); } else { // production stuff services.AddTransient(); } } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseFakeAuthentication(); } else { app.UseRealAuthentication(); } }
第二是使用多个处理程序(如@Tseng所说)。 在这种情况下,我会写这样的东西:
private IHostingEnvironment _env; public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) { _env = env; // other stuff } public void ConfigureServices(IServiceCollection services) { // ... if (_env.IsDevelopment()) { // dev stuff services.AddSingleton(); } else { // production stuff services.AddSingleton(); } }
只需添加一个匿名filter就可以做到这一点,简单易行。
services.AddMvc(opts => { opts.Filters.Add(new AllowAnonymousFilter()); });
参考: https : //www.illucit.com/asp-net/asp-net-core-2-0-disable-authentication-development-environment/
扩展John_J的答案:
public void ConfigureServices(IServiceCollection services) { ... #if DEBUG services.AddMvc(opts => { opts.Filters.Add(new AllowAnonymousFilter()); }); #else services.AddMvc(); #endif }
您可以定义自己的禁用授权的处理程序:
public class DisableAuthorizationHandler : AuthorizationHandler where TRequirement : IAuthorizationRequirement { protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement) { context.Succeed(requirement); return Task.CompletedTask; } }
然后注册:
public void ConfigureServices(IServiceCollection services) { //... #if DEBUG services.AddTransient>(); #endif //... }
- 升级到ASP.NET Core 2.0后,无法从singleton IActiveUsersService使用作用域服务IMongoDbContext
- 引用.NET Core XUnit项目中的标准dll
- 如何在ASP.NET Core中validationDI容器?
- ASP.NET Core中必需的查询字符串参数
- 如何使用UserManager在IdentityUser上加载导航属性
- 使用RequireHttpsAttribute ASP.NET Core的HTTP错误310 ERR_TOO_MANY_REDIRECTS
- 未找到Visual Studio 2015上的ASP.NET 5 addMvc方法
- ASP.NET核心本地化 – 不从资源文件返回值,仅返回Name
- 我在哪里可以记录ASP.NET Core应用程序的启动/停止/错误事件?