如何从Startup :: Configure获取主机Request.PathBase [英] How to get the Host Request.PathBase from Startup::Configure

查看:125
本文介绍了如何从Startup :: Configure获取主机Request.PathBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的启动类public void Configure(IApplicationBuilder app...)中,我以一种非常标准的方式设置我的cookie身份验证:

In my Startup class public void Configure(IApplicationBuilder app...) I'm setting up my cookie authentication in a pretty standard way:

app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
CookieName = cookieName,
//CookiePath = "",
AuthenticationScheme = cookieAuthSchemeName,
}); 

但是我意识到我无法以我想要的方式设置 CookiePath ,因为在此阶段我不知道如何访问主机服务器路径库.例如,如果我的服务器正在监听 http://internal.net:5000/ myappbase /,那么我可以通过一个操作执行一个控制器,我想将Cookie路径设置为/myappbase/controller1 ,但是在该阶段我做不到,因为,我无法获取有关正在侦听/myappbase的服务器的信息.也许我可以使用一些注入的东西,但是我还没有找到它.

But I realized I was unable to set up the CookiePath the way I wanted because I don't know how to access the host server path base at this stage. For instance if my server is listening to http://internal.net:5000/myappbase/ then I could have a controller with one action and I would like to set up my cookie path to be /myappbase/controller1 which I'm failing to do because at that stage, Startup::Configure, I can't get the information about the server listening to /myappbase. Maybe there is something injected that I could use, but I haven't found it yet.

我尝试过的事情:

注入IHttpContextAccessor contextAccessor没有帮助.

我知道,根据我在这里阅读的内容,当我实际上在处理一个请求时,可以在其他任何地方轻松实现:

I known I could be getting that easily anywhere else when I'm actually treating a request base on what I've read here: How can I get the root domain URI in ASP.NET?

我还可以看到,在CookieAuthenticationHandler的代码中,它是基于请求获取此信息的:

And I can also see that in the code of the CookieAuthenticationHandler, it is getting this information based on the Request:

protected string CurrentUri
{
    get
    {
        return Request.Scheme + "://" + Request.Host + Request.PathBase + Request.Path + Request.QueryString;
    }
}

var cookieOptions = new CookieOptions
{
    Domain = Options.CookieDomain,
    HttpOnly = Options.CookieHttpOnly,
    Path = Options.CookiePath ?? (OriginalPathBase.HasValue ? OriginalPathBase.ToString() : "/"),
};

但实际上,在Startup :: Configure中,我不知道该怎么做.

but really, in the Startup::Configure, I don't know how to do that.

任何帮助将不胜感激.

推荐答案

ConfigureServices(IServiceCollection服务)方法中,添加:

services.Configure<CookiePolicyOptions>(options =>
{
    options.OnAppendCookie = (e) =>
    {
        e.CookieOptions.Path = e.Context.Request.PathBase;
    };
    .....
}); 

注意:创建cookie并将其发送到客户端之前,该cookie的路径将设置为Request.PathBase.

Note: when a cookie is created and before it is sent to the client, the path of the cookie will be set to the Request.PathBase.

这篇关于如何从Startup :: Configure获取主机Request.PathBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆