New posture of core cross site login redirection



As a. Net programmer, one of the pains is that since the birth of until the latest core, cross site login redirection can not be realized directly (for example, visit, jump to for login), and only jump to the current site.

Take core for exampleCookieAuthenticationOptions.LoginPathOnly the path can be specified, not the full URL containing the host name. core will automatically add the host name of the current request when redirecting.

.AddCookie(options =>
 options.LoginPath = "/account/signin";

The returnurl query parameter also contains only the path, not the complete URL.

In order to relieve the pain, the antidote we take in the era is either not using the login jump mechanism of, or through a special usercontroller.login action to make a second jump. In the core era, we changed to take the antidote of middleware, and made a jump in the special middleware (which is also troublesome).

After reading the source code of core authentication yesterday, we found a new antidote — modificationCookieAuthenticationEvents.OnRedirectToLoginDelegate to realize cross site login redirection.

Here is the new antidote making method.

Add the following configuration code to addcookie in startup.configureservices to redirect using the modified URL:

.AddCookie(options =>
 var originRedirectToLogin = options.Events.OnRedirectToLogin;
 options.Events.OnRedirectToLogin = context =>
  return originRedirectToLogin(RebuildRedirectUri(context));

The implementation code of rebuildredirecturi is as follows:

private static RedirectContext<CookieAuthenticationOptions> RebuildRedirectUri(
 RedirectContext<CookieAuthenticationOptions> context)
 if (context.RedirectUri.StartsWith(ACCOUNT_SITE))
  return context;

 var originUri = new Uri(context.RedirectUri);
 var uriBuilder = new UriBuilder(ACCOUNT_SITE);
 uriBuilder.Path = originUri.AbsolutePath;
 var queryStrings = QueryHelpers.ParseQuery(originUri.Query);
 var returnUrlName = context.Options.ReturnUrlParameter;
 var returnUrl = originUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped) + queryStrings[returnUrlName];
 uriBuilder.Query = QueryString.Create(returnUrlName, returnUrl).ToString();
 context.RedirectUri = uriBuilder.ToString();
 return context;

The above code is used to implement URL conversion. For details, see

This long-standing pain is finally eliminated relatively gracefully based on the powerful expansion and configuration capabilities of core.


The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.

Recommended Today

Application of tree

Application of tree Storage structure of tree Parental representation (sequential storage) Definition: in each node, the “pointer” (position subscript) pointing to the parents is saved. The root node is fixedly stored in 0, – 1 indicates that there are no parents. Add: add directly without following the logical order. Delete: ① set the pointer to […]