C# Identity 2.0默认项目缺少默认类

C# Identity 2.0默认项目缺少默认类,c#,asp.net-mvc,asp.net-identity,C#,Asp.net Mvc,Asp.net Identity,根据几篇文章;我要指出一点: ,在IdentityConfig.cs中应该有两个助手类 ApplicationUserManager ApplicationRoleManager 我已经在谷歌上搜索了一段时间,似乎不明白为什么我在一个股票MVC5 web应用程序中默认没有ApplicationRoleManager。我还缺少ApplicationUserManager中的方法,例如AddUserToRolesAsync和removeeuserfromsrolesasync。以下代码是我的Ide

根据几篇文章;我要指出一点:

,在IdentityConfig.cs中应该有两个助手类

  • ApplicationUserManager
  • ApplicationRoleManager
  • 我已经在谷歌上搜索了一段时间,似乎不明白为什么我在一个股票MVC5 web应用程序中默认没有
    ApplicationRoleManager
    。我还缺少
    ApplicationUserManager
    中的方法,例如
    AddUserToRolesAsync
    removeeuserfromsrolesasync
    。以下代码是我的
    IdentityConfig.cs
    文件中的全部代码

    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
        }
    
        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };
            // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
            // You can write your own provider and plug in here.
            manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<ApplicationUser>
            {
                MessageFormat = "Your security code is: {0}"
            });
            manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<ApplicationUser>
            {
                Subject = "Security Code",
                BodyFormat = "Your security code is: {0}"
            });
            manager.EmailService = new EmailService();
            manager.SmsService = new SmsService();
            var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return manager;
        }
    }
    
    public class EmailService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // Plug in your email service here to send an email.
            return Task.FromResult(0);
        }
    }
    
    public class SmsService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // Plug in your sms service here to send a text message.
            return Task.FromResult(0);
        }
    }
    
    公共类应用程序管理员:用户管理员
    {
    公共应用程序服务器管理器(IUserStore存储)
    :基地(商店)
    {
    }
    公共静态应用程序SerManager创建(IdentityFactoryOptions选项,IOwinContext上下文)
    {
    var manager=newapplicationUserManager(newuserstore(context.Get());
    //为用户名配置验证逻辑
    manager.UserValidator=新的UserValidator(管理器)
    {
    AllowOnlyAlphanumericUserNames=false,
    RequireUniqueEmail=true
    };
    //配置密码的验证逻辑
    manager.PasswordValidator=新密码验证器
    {
    所需长度=6,
    RequiredOnletterDigit=真,
    RequireDigit=true,
    RequireLowercase=true,
    RequireUppercase=true,
    };
    //注册双因素身份验证提供商。此应用程序使用电话和电子邮件作为接收验证用户代码的步骤
    //您可以编写自己的提供程序并在此处插入。
    manager.RegisterWofactorProvider(“PhoneCode”,新的PhoneNumberTokenProvider
    {
    MessageFormat=“您的安全代码为:{0}”
    });
    manager.RegisterWofactorProvider(“EmailCode”,新的EmailTokenProvider
    {
    Subject=“安全代码”,
    BodyFormat=“您的安全代码为:{0}”
    });
    manager.EmailService=新的EmailService();
    manager.SmsService=新的SmsService();
    var dataProtectionProvider=options.dataProtectionProvider;
    if(dataProtectionProvider!=null)
    {
    manager.UserTokenProvider=newdataprotectortokenprovider(dataProtectionProvider.Create(“ASP.NET标识”);
    }
    退货经理;
    }
    }
    公共类电子邮件服务:IIdentityMessageService
    {
    公共任务SendAsync(IdentityMessage消息)
    {
    //在此处插入电子邮件服务以发送电子邮件。
    返回Task.FromResult(0);
    }
    }
    公共类SMS服务:IIdentityMessageService
    {
    公共任务SendAsync(IdentityMessage消息)
    {
    //在此处插入您的短信服务以发送短信。
    返回Task.FromResult(0);
    }
    }
    
    我相信VS 2013 Update 2附带的更新模板不包含
    ApplicationRoleManager
    类定义。上面引用的文章使用了Microsoft.AspNet.Identity.Samples Nuget包中的代码。这很有意义。我最终使用samples nuget包作为起点。