Asp. NET-to-ASP solution for user authentication cookie

Source: Internet
Author: User

As you migrate your existing user login (sign in) site from ASP. NET core, you will face the question of how to make ASP. NET-core user authentication cookie coexist, so that the ASP. Do the core apps use their own cookies separately? Because ASP. Formsauthentication,asp.net Core uses the claims-based authentication, and their encryption algorithms are different.

The workaround we take is to generate 2 cookies and send them to the client after successful login in ASP.

The claims-based authentication-based authentication cookie that generates ASP. NET core is relatively simple and the sample code is as follows:

var New Claimsidentity (newnew"Basic"); var New ClaimsPrincipal (claimsidentity); await the context. Authentication.signinasync (_cookieauthoptions.authenticationscheme, ClaimsPrincipal);

Generating ASP. FormsAuthentication-based authentication cookies is a little more cumbersome.

The first thing to do is to create a Web API site with ASP. FormsAuthentication to generate a cookie based on the sample code as follows:

 Public Ihttpactionresult Getauthcookie (stringbool  ispersistent) {    var cookie =  Formsauthentication.getauthcookie (LoginName, ispersistent);     return Json (new  {cookie). Name, Cookie. Value, Cookie. Expires});}

Then write a Web API client in the ASP. NET Core login site to get the cookie, the sample code is as follows:

 Public classuserserviceagent{Private Static ReadOnlyHttpClient _httpclient =NewHttpClient ();  Public Static AsyncTask<cookie> Getauthcookie (stringLoginName,BOOLispersistent) {        varResponse =await_httpclient.getasync (URL); Response.        Ensuresuccessstatuscode (); return awaitResponse. Content.readasasync<cookie>(); }}

Finally, an ASP. NET FormsAuthentication cookie is sent specifically to the client in the processing code after the successful login of the ASP. NET Core login site, with the sample code as follows:

var await _userserviceagent.getauthcookie (LoginName, ispersistent); var New cookieoptions () {    = _cookieauthoptions.cookiedomain,    true}; if (cookies.) Expires > datetime.now) {    = cookie. Expires;} Context. Response.Cookies.Append (cookies. Name, Cookie. Value, options);

ASP. NET-to-ASP-user authentication cookie solution

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.