相关疑难解决方法(0)

在C#/ .NET中获取URL的域名

代码:

string sURL = "http://subdomain.website.com/index.htm";
MessageBox.Show(new System.Uri(sURL).Host);
Run Code Online (Sandbox Code Playgroud)

给了我"subdomain.website.com"

但我需要主域名"website.com"用于任何网址或网站链接.

我怎么做?

.net c# uri c#-2.0

13
推荐指数
1
解决办法
2万
查看次数

.Net Core 配置CORS同时允许所有子域和所有localhost端口

我想允许所有内部网站CORS请求到一个共同的内部API( *.myintra.net),并从所有localhost端口(当我们在IIS Express中,即本地调试各种应用程序http://localhost:12345http://localhost:54321等等)。

这个答案向我展示了如何使用SetIsOriginAllowedToAllowWildcardSubdomains()以允许所有子域。

这个答案向我展示了如何localhost使用SetIsOriginAllowed()委托函数来允许任何端口。

但是,这些选项似乎不能一起使用。我的配置:

private bool AllowLocalhost(string origin)
{
    var uri = new Uri(origin);
    return (uri.Host == "localhost");
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        string[] corsList = "https://*.myintra.net,https://some.specificurl.com".Split(",");
        options.AddPolicy("CorsPolicy", builder =>
        {
            builder
            .WithOrigins(corsList.ToArray())
            .SetIsOriginAllowedToAllowWildcardSubdomains()
            .SetIsOriginAllowed(origin => AllowLocalhost(origin)) // disallows calls from myapp.myintra.net since it doesn't uri.Host match "localhost"
            ...();
        });
    });
    ...
}
Run Code Online (Sandbox Code Playgroud)

我可以交换配置的顺序:

.SetIsOriginAllowed(origin => AllowLocalhost(origin))
.SetIsOriginAllowedToAllowWildcardSubdomains()
Run Code Online (Sandbox Code Playgroud)

但是该 …

c# cors asp.net-core asp.net-core-3.1

5
推荐指数
1
解决办法
1272
查看次数

标签 统计

c# ×2

.net ×1

asp.net-core ×1

asp.net-core-3.1 ×1

c#-2.0 ×1

cors ×1

uri ×1