简体   繁体   English

.NET Core 身份登录页面处理程序 OnGetAync()

[英].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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM