When you migrate your existing user login (Sign In) site from Core, you will face the problem of how to enable Core user verification cookies to coexist, so that the app and Core app can use their respective cookies separately? Because it uses FormsAuthentication, Core uses claims-based authentication, and their encryption algorithms are different.
The solution we have adopted is to generate 2 cookies after logging in in Core and send them to the client at the same time.
It is relatively simple to generate Core's claims-based authentication verification cookies, and the sample code is as follows:
var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(, loginName) }, "Basic"); var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); await (_cookieAuthOptions.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties { IsPersistent = isPersistent, ExpiresUtc = (_cookieAuthOptions.ExpireTimeSpan) });
The generated FormsAuthentication-based verification cookies are a little more troublesome.
First, you need to create a Web API site and generate cookies based on FormsAuthentication. The sample code is as follows:
public IHttpActionResult GetAuthCookie(string loginName, bool isPersistent) { var cookie = (loginName, isPersistent); return Json(new { , , }); }
Then write a Web API client to get the cookie in the Core login site. The sample code is as follows:
public class UserServiceAgent { private static readonly HttpClient _httpClient = new HttpClient(); public static async Task<Cookie> GetAuthCookie(string loginName, bool isPersistent) { var response = await _httpClient.GetAsync(url); (); return await <Cookie>(); } }
Finally, in the processing code after logging in the Core login site, the FormsAuthentication cookie is specially sent to the client. The sample code is as follows:
var cookie = await _userServiceAgent.GetAuthCookie(loginName, isPersistent); var options = new CookieOptions() { Domain = _cookieAuthOptions.CookieDomain, HttpOnly = true }; if ( > ) { = ; } (, , options);
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.