使用基于OpenID连接的身份验证来解决此问题。以下是示例代码:
首先,在ASP.net MVC .netframework 4.8中,您需要将身份验证设置为外部登录,以便用户可以使用外部身份验证提供程序进行身份验证。
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = "your_client_id",
Authority = "https://your_authority.com/",
RedirectUri = "http://your_redirect_uri.com",
ResponseType = "id_token",
Scope = "openid profile email",
SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
});
然后在ASP.net MVC Core 6中,您需要编写适当的代码来读取ASP.net MVC .netframework 4.8中设置的Cookie,并将其设置为当前用户身份。
var result = await HttpContext.AuthenticateAsync(
CookieAuthenticationDefaults.AuthenticationScheme);
if (result?.Principal != null)
{
var claims = result.Principal.Claims;
var identity = new ClaimsIdentity(claims, "OpenIdConnect");
HttpContext.SignInAsync(
IdentityConstants.ApplicationScheme,
result.Principal,
new AuthenticationProperties { IsPersistent = false });
}
这样,用户身份将会在ASP.net MVC .netframework 4.8和ASP.net MVC Core 6之间共享。