通过我的.net核心中间件管道



所以我有一个中间件实现了基于基本认证的请求在报头的那一天。然后使用Selenium打开应用程序的浏览器窗口。在这里,我认为我需要实现一些简单的cookie处理,以便能够通过一些链接在网站上点击。到目前为止,它没有任何cookie。感觉这里有一些非常基本的网络流量规则,它一直发送相同的标头。但我不禁想知道什么时候它会回来困扰我,它会一直工作,只要我留在同一个域,或者它会在某些情况下崩溃?

配置方法:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
//app.UseAuthentication();
//app.UseAuthorization();
app.UseMiddleware<BasicAuthMiddleware>();
app.UseBrowserLink();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}

和中间件:

public async Task InvokeAsync(HttpContext context, ILogger<SimpleAuthMiddleware> logger)
{
bool auth = false;
string username;
try
{
var authHeader = AuthenticationHeaderValue.Parse(context.Request.Headers["Authorization"]);
logger.LogInformation("AuthHeader: " + authHeader);
var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
var credentials = Encoding.UTF8.GetString(credentialBytes).Split(new[] { ':' }, 2);
username = credentials[0];
var password = credentials[1];
logger.LogInformation("Found username & password: " + username + ":" + password);
if (username == "testname" && password == "testpass")
{
auth = true;
}
}
catch (Exception)
{
}
if (auth)
{
logger.LogInformation("Auth Passed");
await _next(context);
}
else
{
logger.LogInformation("Auth Failed");
context.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
return;
}
}

我很确定这不会回来困扰你,因为根据你的代码,我看到你已经禁用了app.UseAuthentication();app.UseAuthorization();,并使用你的自定义身份验证(这实际上不是身份验证,因为它没有设置代表和坚持使用HttpRequest的User信息,它只是允许请求通过)中间件。同样,不是通过cookie验证用户,而是在header上验证用户。

所以…即使cookie在那里,也没关系,因为你从一开始就没有使用它。

最新更新