Bob*_*ard 5 c# asp.net-core asp.net-core-2.0
我试图找到一种方法来阻止我的 aspnetcore 应用程序将“?ReturnUrl=”添加到 URL。有谁知道怎么做,使用某种中间件。
我尝试像下面那样做,但没有任何效果:
public class RequestHandlerMiddleware
{
private readonly RequestDelegate _next;
public RequestHandlerMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if(context.Request.QueryString.HasValue && context.Request.QueryString.Value.Contains("?ReturnUrl="))
{
context.Request.QueryString = new QueryString(string.Empty);
}
await _next.Invoke(context);
}
}
public static class RequestHandlerMiddlewareExtension
{
public static IApplicationBuilder UseRequestHandlerMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<RequestHandlerMiddleware>();
}
}
Run Code Online (Sandbox Code Playgroud)
注册startup.cs:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/error");
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseAuthentication();
app.UseRequestHandlerMiddleware();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
}
Run Code Online (Sandbox Code Playgroud)
最后,我还尝试了旧帖子中关于 .NET 框架相同问题的一些(调整)方法(在 stackoverflow 上),但也失败了
编辑:除了“标准”[Authorize] 属性之外,我没有使用任何其他 AuthorizationAttribute / Handler。仅有的:
services.AddAuthorization();
Run Code Online (Sandbox Code Playgroud)
编辑 2:我完全忘记了我还在应用程序的其他地方注册了一部分启动,因为它是共享的:
public static IServiceCollection Load(IServiceCollection services, IConfiguration config)
{
services.AddDbContext<SqlContext>(options =>
{
options.UseSqlServer(config.GetConnectionString("DefaultConnection"));
});
services.AddIdentity<User, Role>(options =>
{
options.Lockout = new LockoutOptions
{
AllowedForNewUsers = true,
DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30),
MaxFailedAccessAttempts = 5
};
})
.AddEntityFrameworkStores<SqlContext>()
.AddDefaultTokenProviders()
.AddUserStore<UserStore<User, Role, SqlContext, Guid>>()
.AddRoleStore<RoleStore<Role, SqlContext, Guid>>()
.AddUserManager<ApplicationUserManager>();
services.Configure<IdentityOptions>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 5;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = true;
});
services.ConfigureApplicationCookie(options =>
options.Events = new CookieAuthenticationEvents
{
OnRedirectToLogin = ctx =>
{
if (ctx.Request.Path.StartsWithSegments("/api") &&
ctx.Response.StatusCode == (int)HttpStatusCode.OK)
{
ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
else if (ctx.Response.StatusCode == (int)HttpStatusCode.Forbidden)
{
ctx.Response.StatusCode = (int)HttpStatusCode.Forbidden;
}
else
{
ctx.Response.Redirect(ctx.RedirectUri);
}
return Task.FromResult(0);
}
});
return services;
}
Run Code Online (Sandbox Code Playgroud)
我首先想到的是:
[HttpGet]
public IActionResult LogIn()
{
if (!string.IsNullOrEmpty(Request.QueryString.Value))
return RedirectToAction("Login");
return View();
}
Run Code Online (Sandbox Code Playgroud)
这将从 URL 中删除 QueryString 部分,以便“ReturnUrl”不会在用户地址栏上停留太久,并且会拒绝任何 QueryString。
更好的解决方法是创建您自己的 AuthorizeAttribute 版本,它不会将 ReturnUrl 放入 QueryString,但似乎随着新的基于策略的授权方法的出现,不鼓励自定义 AuthorizeAttribute。更多的
使用基于策略的方法并创建自定义AuthorizationHandler.
(我一试用就会发布更新)
| 归档时间: |
|
| 查看次数: |
2273 次 |
| 最近记录: |