如何在asp.net mvc中启用多种帐户类型

本文关键字:类型 启用 asp net mvc | 更新日期: 2023-09-27 17:57:59

我正在创建一个MVC站点,其中应该有两种类型的用户。出于简单的目的,用户是(User,Sales)。这就是一个名为Steve的用户应该能够作为一个用户或销售人员作为一个单独的帐户存在。由于asp.net身份只允许1个用户配置文件和注册/登录流,我如何用它来建模?我需要两个独立的站点吗?

我读过我想做的是创建一个多租户应用程序,但我还没有看到任何关于asp.net mvc的好例子。理想情况下,我希望用户访问www.foo.com并消费Sales用户在www.foo.com/Sales上创建的"东西"。用户将在www.foo.com注册并拥有自己的流程,用户将在http://www.foo.com/Sales注册并拥有他们自己的产品创建流程。

使用EF代码优先和ASP.net身份对此进行建模的最佳方法是什么

我的想法:

  • 2个独立的站点-但是我可以通过类库共享每个站点上的2个DBContext吗

如何在asp.net mvc中启用多种帐户类型

出于您的目的,您应该使用自定义成员资格提供程序。这是关于它的文章链接http://logcorner.wordpress.com/2013/08/29/how-to-configure-custom-membership-and-role-provider-using-asp-net-mvc4/.此外还有"如何为ASP创建自定义成员资格提供程序"。NET MVC 2?关于如何创建自定义会员资格提供商

自定义会员资格提供商的示例(请注意,这是我的旧代码,所以你应该尝试优化它):

public class UsersProvider : RoleProvider
 {
     public override void AddUsersToRoles(string[] username, string[] roleName)
    {
        using (SmaCareEntities db = new SmaCareEntities())
        {
            List<int> ulist = (from u in db.Users
                               where username.Contains(u.UserName)
                               select u.RoleId).ToList();
            List<int> rlist = (from r in db.Roles
                               where roleName.Contains(r.Name)
                               select r.Id).ToList();

            var urlist = (from r in rlist
                          select new Role { Id = r }).FirstOrDefault();
            db.Roles.Attach(urlist);
            db.ObjectStateManager.ChangeObjectState(urlist, EntityState.Modified);
            db.SaveChanges();
        }
    }
    public override string ApplicationName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public override void CreateRole(string roleName)
    {
        throw new NotImplementedException();
    }
    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        throw new NotImplementedException();
    }
    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
        using (SmaCareEntities db = new SmaCareEntities())
        {
            User user = db.Users.FirstOrDefault(u => u.UserName.Equals(usernameToMatch, StringComparison.CurrentCultureIgnoreCase) || u.Email.Equals(usernameToMatch, StringComparison.InvariantCultureIgnoreCase));
            var roles = from r in db.Roles
                        where user.RoleId == r.Id
                        select r.Name;
            if (roles != null)
                return roles.ToArray();
            else return null;
        }
    }
    public override string[] GetAllRoles()
    {
        throw new NotImplementedException();
    }
    public override string[] GetRolesForUser(string username)
    {
        using (SmaCareEntities db = new SmaCareEntities())
        {
            User user = db.Users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase) || u.Email.Equals(username, StringComparison.InvariantCultureIgnoreCase));
            var roles = user.Role.Name;
            if (roles != null)
                return new string[] {roles};
            else
                return new string[] { };
        }
    }
    public override string[] GetUsersInRole(string roleName)
    {
        throw new NotImplementedException();
    }
    public override bool IsUserInRole(string username, string roleName)
    {
        using (SmaCareEntities db = new SmaCareEntities())
        {
            User user = db.Users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase) || u.Email.Equals(username, StringComparison.CurrentCultureIgnoreCase));
            if (user != null)
            {
                var roles = user.Role.Name;
                if (user != null)
                    return roles.Equals(roleName, StringComparison.CurrentCultureIgnoreCase);
                else
                {
                    return false;
                }
            }
            else
                return false;
        }
    }
    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
    {
        throw new NotImplementedException();
    }
    public override bool RoleExists(string roleName)
    {
        throw new NotImplementedException();
    }
}

用法示例:

    [Authorize(Roles = "Administrator")]   // this is the line where your membership provider checks if current user in "Administrator" role
    public ActionResult ProductList(string keyword, int? page)
    {
        return View(MainService.GetProducts(keyword, page));
    }
    [Authorize(Roles = "Administrator")]
    public ActionResult Delete(int id)
    {
        MainService.Delete(id);
        return RedirectToAction("ProductList");
    }

不要忘记将其添加到您的web.config文件中:

<roleManager enabled="true" defaultProvider="UsersProvider"> <providers> <clear/> <add name="UsersProvider" type="BusinessLogic.Users.UsersProvider" /> </providers>

此外,在您的数据库中,应该有一个User表和一个具有1到N关系的Role表(在User表中使用foreign key - Id_role