我有内置在.NET 6 核心中的 Azure 应用程序服务。该应用程序服务具有可供所有人访问的自定义域。但是所有人都无法访问默认域,它引发了
403 forbidden
错误。
现在我的问题是,当我在某些控制器方法上使用 Authorize 过滤器时,应用程序将我重定向到它的默认域,如 https://app-xxxxx.azurewebsites.net/Home/Login 所以它抛出
Error 403 - Forbidden
异常。它应该将我的自定义域重定向到我https://dev.xxxxx.com/Home/Login.
[Authorize]
public ActionResult Dashboard()
{}
如何告诉应用程序重定向到其自定义域?有人可以帮我解决这个问题吗?我在 .NET 6 核心 c# 中工作。
通过将
location header
中的主机名配置为自定义域名,将借助Authorize filter
中的Azure App Service
实现重定向到自定义域而不是默认域。
按照以下步骤进行。
Create a rewrite rule with a condition that evaluate if the location header in the response contains azurewebsites.net.
它还必须执行一个操作来重写位置标头以具有自定义域的主机名。
您可以配置
Application Gateway
和 App Service
不覆盖主机名。
对于使用应用程序网关配置应用程序服务中的自定义域。
通过编写自定义规则来实现。
public void ApplyRule(RewriteContext context)
{
var response = context.HttpContext.Response;
if (response.StatusCode == (int)HttpStatusCode.Redirect && response.Headers.ContainsKey("Location"))
{
var loc = response.Headers["Location"].ToString();
if (loc.Contains("azurewebsites.net"))
{
var custom_Domain = "https://dev.xxxxx.com";
response.Headers["Location"] = loc.Replace("https://app-xxxxx.azurewebsites.net", custom_Domain);
}
}
context.Result = RuleResult.ContinueRules;
}
自定义规则类
或者在
Startup.cs
中使用以下代码
app.Use(async (context, next) =>
{
context.Response.Redirect("https://dev.xxxxx.com");
await next();
});
app.Use(async (context, next) =>
{
context.Response.Redirect("https://dev.xxxxx.com/Home/Login");
await next();
});