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