如何在控制器或视图中访问登录路径



我有这样的东西:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies",
LoginPath = new PathString("/Account/Login/"),
...
});

如何访问代码中其他位置(控制器、视图等(设置为登录路径的内容?例如,我想在一个控制器方法中将我的登录URL传递给我的SPA(返回JSON(。或者,在Razor视图中,我想创建一个登录按钮。

当然,我可以复制字符串,但我想知道是否有办法访问CookieAuthenticationOptions?

您可以获得此值,例如:

public HomeController(IOptionsMonitor<CookieAuthenticationOptions> cookieAuthenticationOptions)
{
_cookieAuthenticationOptions = cookieAuthenticationOptions.Get(CookieAuthenticationDefaults.AuthenticationScheme);
var loginPath = _cookieAuthenticationOptions.LoginPath;
}

最新更新