我有一台ASP Net Core
服务器,在身份验证成功后,该服务器应重定向到Web应用程序的主页。
Authentication
和Authorization
通过ASP NET Core middleware
完成。
但是,在身份验证成功后,用户将在中间件内部循环:
启动
public void ConfigureServices(IServiceCollection services) {
services.AddCors();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(
options => {
options.LoginPath = "/";
options.AccessDeniedPath = "/AccessDenied";
options.Events.OnRedirectToLogin = (context) => {
context.Response.StatusCode = 401;
return Task.CompletedTask;
};
});
services.AddMvc();
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
app.UseAuthentication();
app.UseMiddleware<MultiAuthWare>();
app.UseMvc(routes => {
routes.MapRoute(name: "default", template: "{controller}/{action}/{id?}");
});
}
中间件
public async Task Invoke(HttpContext context) {
string token = context.Request.Query["token"];
var service = HttpUtility.UrlEncode("http://"+context.Request.Host.Value);
var serviceName = HttpUtility.UrlEncode(APPLICATION_NAME);
bool isAuth;
if (token==null || token.Length == 0) {
string redirectURL = $"{MULTIAUTH_SERVICE_URL}?serviceURL={service}&serviceName={serviceName}";
context.Response.Redirect(redirectURL,true);
return;
}
bool isAuth=await Something());
//the problem is down here !
if(isAuth)
{
Claim claim = new Claim("tok", token);
ClaimsPrincipal claims = new ClaimsPrincipal();
claims.AddIdentity(new ClaimsIdentity(new[] { claim }));
await AuthenticationHttpContextExtensions.SignInAsync(context, claims);
string resourceRequest = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path}";
context.Response.Redirect(resourceRequest);
}
}
其工作方式示例:
-如果用户需要页面http://localhost:8300
,则他将被重定向到第三方服务(方法的第一部分,直到我发表评论)。
-下次他请求页面时,中间件检查其token
(isAuth
变量)。
-如果布尔值是正确的(在我测试的案例中是这样),我希望他使用关联的令牌重定向到http://localhost:8300/home
。
我的渗透有什么问题?
PS 我的问题是在中间件中-方法的第二部分(在注释之后)。
用户第一次需要访问该网站时,他执行方法的第一部分并返回。
他第二次已经提出一个令牌,并且在Invoke
的末尾应将token
重定向到他请求的资源。
但是事实并非如此,他一直在中间件中循环。