【问题标题】:unable to add roles in asp.net identity无法在 asp.net 身份中添加角色
【发布时间】:2018-05-05 13:44:16
【问题描述】:

我正在使用以下代码来添加用户的角色。

    Roles.AddUserToRole(model.Email, "COMPANYVIP");

然后我得到了这个错误:

    The Role Manager feature has not been enabled

经过一番研究,我发现我们必须在 web.config 中添加以下连接字符串

    <configuration>
      <system.web>
        <roleManager enabled="true" />
      </system.web>
    </configuration>

添加这个消除了我的第一个错误,但现在我得到了这个错误:

    A network-related or instance-specific error occurred while establishing a connection to SQL Server

我现在该怎么办?

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-identity


    【解决方案1】:

    删除您在web.configStartup.Auth 中的更改,将以下引用添加到ConfigureAuth

    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        // Add this reference to RoleManager (without changing any other items)
        // Make sure it is added below ApplicationDbContext.Create
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
    }
    

    然后在你的 Controller 中,确保它在构造函数中包含这个:

    public class YourController : Controller
    {
        // Add this
        private ApplicationRoleManager _roleManager;
    
        // Add roleManager
        public YourController(ApplicationRoleManager roleManager)
        {
            // Add this
            RoleManager = roleManager;
        }
    
        public ApplicationRoleManager RoleManager {
            get {
                return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
            }
            private set {
                _roleManager = value;
            }
        }
    }
    

    并将其包含在控制器的 Dispose 中(如果有的话):

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            // include this
            if (_roleManager != null)
            {
                _roleManager.Dispose();
                _roleManager = null;
            }
        }
    
        base.Dispose(disposing);
    }
    

    您可能还需要将此代码添加到IdentityConfig(如果您使用模板,则在 App_Start 文件夹中):

    public class ApplicationRoleManager : RoleManager<IdentityRole>
    {
        public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
        : base(roleStore)
        { }
    
        public static ApplicationRoleManager Create(
            IdentityFactoryOptions<ApplicationRoleManager> options,
            IOwinContext context)
        {
            var manager = new ApplicationRoleManager(
                new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
            return manager;
        }
    }
    

    您现在应该可以在 Controller 中使用 RoleManager。

    【讨论】:

    • 感谢您的回复。但正如我所说的那样。我的库中没有 ApplicationRoleManger 类。我搜索并得到了这个链接link,然后我按照建议手动添加了这个类。现在我得到另一个像这样的错误 *Microsoft.AspNet.Identity.EntityFramework.dll 中发生了“System.ArgumentNullException”类型的异常,但没有在用户代码中处理我哪里做错了?
    • @Yogesh Gautam 我已经更新了答案以包含对 RoleManager 定义的引用
    • 在实现 var manager = new ApplicationRoleManager( new RoleStore (context.Get())); 时我再次得到“值不能为空错误”
    • 以上问题解决。我的错误是我在创建上下文之前在 ConfigureAuth 方法中添加了 applicationrolemanage 。所以它显示了空值。
    • 但是,在将用户添加到角色时,我的问题仍然没有解决,它被处理并显示此错误“角色管理器功能尚未启用。” .
    猜你喜欢
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 2017-01-07
    • 2014-08-08
    • 1970-01-01
    相关资源
    最近更新 更多