ASP. NET Core for a new antidote to cross-site login redirection

Source: Internet
Author: User

As a. NET programmer, one of the pain is that since the date of the birth of ASP. The latest ASP. Login redirection is not possible directly (e.g. access https://q.cnblogs.com, jump to https:// passport.cnblogs.com to log in), you can only jump to the current site.

In the case of ASP. Cookieauthenticationoptions.loginpath can only specify a path and cannot specify the full URL that contains the hostname, and ASP. NET core automatically adds the host name of the current request when the redirect takes place.

==    {"/account/signin";});

The RETURNURL query parameter also contains only the path and does not contain the full URL.

In order to understand the pain, the antidote we take in the ASP. NET is either without the login jump mechanism of ASP. Or two jumps through a dedicated usercontroller.login Action, we changed to Middlewar in the ASP. E of the antidote, in the specialized middleware jump (also more troublesome).

Yesterday, after reading the source of ASP. NET Core authenticaion, we found a new antidote-modifying the Cookieauthenticationevents.onredirecttologin delegate for cross-site login redirection.

Here's how to make the new antidote.

In Startup.configureservices, add the following configuration code to Addcookie to redirect using the modified URL:

==    {var originredirecttologin = options. Events.onredirecttologin;     = Context =    {        return  originredirecttologin (Rebuildredirecturi (context));};    }); 

The implementation code for Rebuildredirecturi is as follows:

Private StaticRedirectcontext<cookieauthenticationoptions>Rebuildredirecturi (Redirectcontext<CookieAuthenticationOptions>context) {    if(context. Redirecturi.startswith (account_site))returncontext; varOriginuri =NewUri (context.    Redirecturi); varUriBuilder =NewUriBuilder (Account_site); Uribuilder.path=Originuri.absolutepath; varQuerystrings =queryhelpers.parsequery (originuri.query); varReturnurlname =context.    Options.returnurlparameter; varRETURNURL = Originuri.getcomponents (Uricomponents.schemeandserver, uriformat.unescaped) +Querystrings[returnurlname]; Uribuilder.query=querystring.create (Returnurlname, RETURNURL).    ToString (); Context. Redirecturi=uribuilder.tostring (); returncontext;}

Above a bunch of code for the implementation of the URL conversion, see Bo asked https://q.cnblogs.com/q/108087/

This long-standing pain is based on the relative elegance of ASP. NET Core's powerful expansion and configuration capabilities.

ASP. NET Core for a new antidote to cross-site login redirection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.