[英].NET Core Identity Login Page handler OnGetAync()
I working on a .NET Core 3.1 project using Identity.我使用 Identity 处理 .NET Core 3.1 项目。 On the
Login.cs.html
page, the OnGetAsync()
page handler is as follows:在
Login.cs.html
页面上, OnGetAsync()
页面处理程序如下:
public async Task OnGetAsync(string returnUrl = null)
{
if (!string.IsNullOrEmpty(ErrorMessage))
{
ModelState.AddModelError(string.Empty, ErrorMessage);
}
returnUrl = returnUrl ?? Url.Content("~/");
// Clear the existing external cookie to ensure a clean login process
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync())
.ToList();
ReturnUrl = returnUrl;
}
I'm a bit confused about the line of code returnUrl = returnUrl ?? Url.Content("~/");
我对代码行
returnUrl = returnUrl ?? Url.Content("~/");
returnUrl = returnUrl ?? Url.Content("~/");
and why it is written this way.以及为什么这样写。 Why is it checking if
returnUrl
is null
, won't it always be null
?为什么要检查
returnUrl
是否为null
,它不会总是为null
吗? Why not remove the string returnUrl = null
as a method parameter and assign ReturnUrl = Url.Content("~/");
为什么不删除
string returnUrl = null
作为方法参数并分配ReturnUrl = Url.Content("~/");
? ? Am I missing some thing here?
我在这里错过了什么吗?
The returnUrl
parameter has been set up to use an optional argument : returnUrl
参数已设置为使用可选参数:
If no argument is sent for that parameter, the default value is used.
如果没有为该参数发送参数,则使用默认值。
With the default setup for Identity, the value for returnUrl
comes from the query-string of the request URL.对于 Identity 的默认设置,
returnUrl
的值来自请求 URL 的查询字符串。 For example, the following URL provides a value for the returnUrl
argument:例如,以下 URL 为
returnUrl
参数提供了一个值:
/Identity/Account/Login?returnUrl=/Some/Protected/Route
In this case, returnUrl
will not be null
.在这种情况下,
returnUrl
不会为null
。 However, consider the following URL:但是,请考虑以下 URL:
/Identity/Account/Login
In this case, returnUrl
will be null
.在这种情况下,
returnUrl
将为null
。 There's no URL to redirect to after the login process completes, so the line you've called out will set it to Url.Content("~/")
.登录过程完成后没有要重定向到的 URL,因此您调用的行会将其设置为
Url.Content("~/")
。 That just means it'll end up redirecting the user to the root of the web application, which is typically the home page.这只是意味着它最终会将用户重定向到 Web 应用程序的根目录,通常是主页。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.