ASP.netMVC.netframework4.8和ASP.netMVCCore6之间共享用户身份验证无法正常工作
创始人
2024-09-19 02:30:34
0

在ASP.net MVC .netframework 4.8中,使用的身份验证方式是System.Web.Security.FormsAuthentication,而在ASP.net MVC Core 6中使用的是Microsoft.AspNetCore.Authentication。

要实现两个版本之间的用户身份验证共享,可以采用以下步骤:

  1. 在ASP.net MVC .netframework 4.8中,使用System.Web.Security.FormsAuthentication将用户身份验证令牌以cookie的方式保存在浏览器中。

  2. 在ASP.net MVC Core 6中,使用Microsoft.AspNetCore.Authentication.Cookies将从ASP.net MVC .netframework 4.8中传递过来的用户身份验证令牌解密,并验证身份。

在ASP.net MVC .netframework 4.8中的代码示例:

var ticket = new FormsAuthenticationTicket(
    1,
    user.UserName,
    DateTime.Now,
    DateTime.Now.AddMinutes(30),
    false,
    userData,
    FormsAuthentication.FormsCookiePath);

var encryptedTicket = FormsAuthentication.Encrypt(ticket);

var cookie = new HttpCookie(
    FormsAuthentication.FormsCookieName,
    encryptedTicket);

HttpContext.Response.Cookies.Add(cookie);

在ASP.net MVC Core 6中的代码示例:

services
    .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.Cookie.Name = FormsAuthentication.FormsCookieName;
        options.Cookie.Path = FormsAuthentication.FormsCookiePath;
        options.TicketDataFormat =
            new FormsAuthenticationTicketDataFormat(
                FormsAuthentication.FormsCookieName);
    });

public class FormsAuthenticationTicketDataFormat :
    ISecureDataFormat
{
    private readonly string _cookieName;
    private readonly FormsAuthenticationTicketSerializer _serializer;

    public FormsAuthenticationTicketDataFormat(string cookieName)
    {
        _cookieName = cookieName;
        _serializer = new FormsAuthenticationTicketSerializer();
    }

    public string Protect(AuthenticationTicket data)
    {
        throw new NotImplementedException();
    }

    public AuthenticationTicket Unprotect(string protectedText)
    {
        var cookie = HttpContext.Request.Cookies[_cookieName];
        var ticket = FormsAuthentication.Decrypt(cookie.Value);

        return _serializer.Deserialize(ticket.UserData);
    }
}

相关内容

热门资讯

透视美元局(wepoker)外... 1、透视美元局(wepoker)外挂透明挂辅助插件(wepoke辅助德之星)其实有挂(详细透视科技教...
透视软件(Wepoke)外挂透... 透视软件(Wepoke)外挂透明挂辅助app(wepokeai辅助)好像是真的有挂(详细透视辅助教程...
透视规律(wepoke计算辅助... 透视规律(wepoke计算辅助)聚星扑克德州有假吗(详细辅助黑科技教程)其实是真的有挂1)wepok...
德州ai辅助!菠萝德州app挂... 德州ai辅助!菠萝德州app挂哪里买,(wEPOKE)本来是真的有挂(详细透视新版2025教程);1...
透视app(wepoker)透... 1、透视app(wepoker)透视辅助挂(wepoke真的有挂)本来真的有挂(详细透视线上教程)。...
透视存在(德州ai人工智能)a... 透视存在(德州ai人工智能)aapoker透明挂多久被封(详细辅助微扑克教程)最初是真的有挂1.德州...
透视真的(wEpoKe)透明挂... 自定义wepoke辅助插件系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,...
德州ai机器人!微扑克机器人代... 1、德州ai机器人!微扑克机器人代打俱乐部,(wEPOKE)原先存在有挂(详细辅助德州教程);详细教...
透视透视(we辅助poker德... 透视透视(we辅助poker德之星)轰趴大菠萝十三水辅助(详细辅助插件教程)一贯存在有挂是一款可以让...
智星德州菠萝开挂!we辅助po... 1、智星德州菠萝开挂!we辅助poker德之星,(WepOke)原本是有挂(详细辅助透牌教程)(UU...