C# 将ASP.NET MVC成员身份转换为ASP.NET核心标识系统

C# 将ASP.NET MVC成员身份转换为ASP.NET核心标识系统,c#,asp.net-mvc,asp.net-core,asp.net-identity,asp.net-membership,C#,Asp.net Mvc,Asp.net Core,Asp.net Identity,Asp.net Membership,我有一个使用ASP.NETMVC和会员资格的网站。我添加了另一个ASP.NET核心API项目,我想使用新的标识系统 我当前的会员登录方式如下: FormsAuthentication.Initialize(); DateTime expire = DateTime.Now.AddDays(2); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, model.UserName + "^" + model.

我有一个使用ASP.NETMVC和会员资格的网站。我添加了另一个ASP.NET核心API项目,我想使用新的标识系统

我当前的会员登录方式如下:

 FormsAuthentication.Initialize();
 DateTime expire = DateTime.Now.AddDays(2);

 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, model.UserName + "^" + model.Name, DateTime.Now,expire,true,string.Empty, FormsAuthentication.FormsCookiePath);

 string encryptedTicket = FormsAuthentication.Encrypt(ticket);

 HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
 authCookie.Expires = expire;
 Response.Cookies.Add(authCookie);
我向系统中添加如下角色(在Global.asax中):

受保护的无效FormsAuthentication\u onAuthentication(对象发送方,FormsAuthenticationEventArgs e)
{
if(FormsAuthentication.CookiesSupported==true)
{
if(Request.Cookies[FormsAuthentication.FormScookeName]!=null)
{
var_repoUser=DependencyResolver.Current.GetService();
字符串用户名=FormsAuthentication
.Decrypt(Request.Cookies[FormsAuthentication.FormScookeName].Value).Name;
var un=username.ToMobile();
字符串角色=_repoUser.Where(p=>p.UserName==un.Single().roles;
e、 用户=新的GenericPrincipal(新的
System.Security.Principal.GenericEntity(用户名),roles.Split(',');
}
}
}
我的用户模型是这样的


我的问题是:在我的ASP.NET核心项目中,如何使用该用户模型实现新的身份系统?

所以你没有角色模型?您的角色是用户模型中的一个字段,它与“,”分隔开?是的,就是@keyone2693您没有角色模型吗?您的角色是用户模型中的一个字段,它与“,”分隔开?是的,正是@keyone2693
 protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
 {
        if (FormsAuthentication.CookiesSupported == true)
        {
            if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
            {
                    var _repoUser = DependencyResolver.Current.GetService<IUserRepository>();

                    string username = FormsAuthentication
                     .Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                    var un = username.ToMobile();
                    string roles = _repoUser.Where(p => p.UserName == un).Single().Roles;

                    e.User = new GenericPrincipal(new 
                     System.Security.Principal.GenericIdentity(username), roles.Split(','));
            }
        }
    }