如何在LocalDB的ASP.NET MVC 5 Web应用程序中使用角色?

时间:2015-08-10 16:43:42

标签: sql-server asp.net-mvc localdb

我想使用app_data文件夹中 LocalDB 数据库文件中的默认ASP.NET MVC 5成员资格来管理用户和角色。如果它不存在,则会自动创建。

我编写了一个角色编辑器,但是当我尝试将角色应用于Web API时,如下所示:

[Authorize(Roles= "SystemAdmin")]

,角色管理器尝试在数据库中调用存储过程。

Invalid object name 'dbo.aspnet_SchemaVersions'.

此存储过程通常在由 aspnet_regsql 实用程序创建的完整SQL Server成员资格数据库中创建,但 aspnet_regsql 实用程序仅在完整的SQL Server数据库上运行,不在LocalDB数据库上。

有没有办法告诉角色提供者不要在不必从头开始编写角色提供程序的情况下进行此存储过程调用?我的数据库连接定义如下 -

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\DefaultConnection.mdf;Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient" />

1 个答案:

答案 0 :(得分:0)

我已经编写了一个替代解决方案 - 一个与 LocalDB 一起使用的自定义RoleProvider。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using MyApp.Models;

namespace MyAppNamespace
{

    /// <summary>
    /// The purpose of this role provider is to support membership + roles in a MSSQL LocalDB.
    /// It works around the limitation of the standard SqlRoleProvider
    /// which requires stored procedures in a SQL Server database.
    /// </summary>
    public class CustomRoleProvider : RoleProvider
    {
        public override string ApplicationName { get; set; }


        public override string[] GetRolesForUser(string username)
        {
            using (var usersContext = new ApplicationDbContext())
            {
                var user = usersContext.Users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase) || u.Email.Equals(username, StringComparison.CurrentCultureIgnoreCase));


                if (user == null) return new string[] { };

                var roles = from ur in user.Roles
                            from r in usersContext.Roles
                            where ur.RoleId == r.Id
                            select r.Name;
                if (roles != null)
                    return roles.ToArray();
                else
                    return new string[] { };
            }

        }


        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            foreach(var userName in usernames)
            {
                var roles = GetRolesForUser(userName);
                foreach(var roleName in roleNames)
                {
                    if (!roles.Contains<string>(roleName))
                    {
                        using (var usersContext = new ApplicationDbContext())
                        {
                            var user = usersContext.Users.FirstOrDefault(u => u.UserName.Equals(userName, StringComparison.CurrentCultureIgnoreCase));
                            if (user != null)
                            {
                                var role = usersContext.Roles.FirstOrDefault(r => r.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));
                                if (role != null)
                                {
                                    var userRole = new Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole();
                                    userRole.RoleId = role.Id;
                                    userRole.UserId = user.Id;
                                    user.Roles.Add(userRole);
                                }
                            }
                            usersContext.SaveChanges();
                        }
                    }
                }
            }
        }


        public override string[] GetAllRoles()
        {
            using (var usersContext = new ApplicationDbContext())
            {
                return usersContext.Roles.Select(r => r.Name).ToArray();
            }
        }


        public override bool IsUserInRole(string username, string roleName)
        {
            return this.GetRolesForUser(username).Contains(roleName);
        }

        public override void CreateRole(string roleName)
        {
            var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
            role.Id = Guid.NewGuid().ToString();
            role.Name = roleName;

            using (var usersContext = new ApplicationDbContext())
            {
                usersContext.Roles.Add(role);
                usersContext.SaveChanges();
            }

        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            var usersInRole = GetUsersInRole(roleName);
            if (throwOnPopulatedRole)
            {
                if (usersInRole.Length > 0)
                {
                    throw new Exception("Role " + roleName + " is not empty");
                }
            }

            var roleNameArray = new string[1];
            roleNameArray[0] = roleName;
            RemoveUsersFromRoles(usersInRole, roleNameArray);

            using (var usersContext = new ApplicationDbContext())
            {
                var role = usersContext.Roles.FirstOrDefault(r => r.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));
                if (role != null)
                {
                    usersContext.Roles.Remove(role);
                    usersContext.SaveChanges();
                    return true;
                }
                return false;
            }

        }

        public override bool RoleExists(string roleName)
        {
            using (var usersContext = new ApplicationDbContext())
            {
                var role = usersContext.Roles.FirstOrDefault(r => r.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));
                return (role != null);
            }
        }


        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {

            foreach (var userName in usernames)
            {
                var roles = GetRolesForUser(userName);
                foreach (var roleName in roleNames)
                {
                    if (!roles.Contains<string>(roleName))
                    {
                        using (var usersContext = new ApplicationDbContext())
                        {
                            var user = usersContext.Users.FirstOrDefault(u => u.UserName.Equals(userName, StringComparison.CurrentCultureIgnoreCase));
                            if (user != null)
                            {
                                var role = usersContext.Roles.FirstOrDefault(r => r.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));
                                if (role != null)
                                {
                                    var userRole = new Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole();
                                    userRole.RoleId = role.Id;
                                    userRole.UserId = user.Id;
                                    user.Roles.Remove(userRole);
                                }
                            }
                            usersContext.SaveChanges();
                        }
                    }
                }
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            using (var usersContext = new ApplicationDbContext())
            {
                var role = usersContext.Roles.FirstOrDefault(r => r.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));

                var users = from ur in role.Users
                            from u in usersContext.Users
                            where ur.RoleId == u.Id
                            select u.UserName;
                if (users != null)
                    return users.ToArray();
                else
                    return new string[] { };
            }
        }


        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            var regEx = new System.Text.RegularExpressions.Regex(usernameToMatch);

            using (var usersContext = new ApplicationDbContext())
            {
                var role = usersContext.Roles.FirstOrDefault(r => r.Name.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));

                var users = from ur in role.Users
                            from u in usersContext.Users
                            where ur.RoleId == u.Id
                            && regEx.IsMatch(u.UserName)
                            select u.UserName;
                if (users != null)
                    return users.ToArray();
                else
                    return new string[] { };
            }

        }
    }

}