我从 .NET 4 上的 VB.NET Webform 转向 C# 和 ASP.NET Core 6 Razor 页面。
我们的用户通过注入标头中的网络信息进行身份验证。
之前我们会检查
global.asax
中的有效标头。如果有效,我们将允许他们查看他们输入的地址。
用户转到
myAwesomePage.aspx
'global.asax
If HttpContext.Current.Session Is Nothing then
'log the user in using header data
else
'go to newAccount.aspx
end if
在 Razor 中,我似乎无法允许用户访问
myAwesomePage.cshtml
//program.cs
builder.Services.AddAuthentication("myApp")
.AddCookie("myApp", options =>
{
options.Cookie.Name = "myApp";
options.LoginPath = "/Account/Login";
// can I get the intended page here somehow? I don't think I can...
});
//login
public async Task<IActionResult> OnGetAsync(){
string referer = HttpContext.Request.Headers["Referer"].toString(); //this is always "" so I can't use it to redirect the user.
//other code
}
如何使用 razor 验证用户身份并重定向?短暂性脑缺血发作。
我从这里得到的原始尝试:Asp.Net Core url引用为空不起作用。 改变
HttpContext.Request.Headers["Referer"].toString();
到
HttpContext.Request.Query["returnUrl"]
问题解决了。
Headers["Referer"]
与 Query["returnUrl"]
是另一天的问题。