我正在创建一个MVC网站,其中应该有两种类型的用户。出于简单目的,用户是(用户,销售)。这是一个名为Steve的用户应该能够作为User或Sales作为单独的帐户存在。我如何使用asp.net身份进行建模,因为它只允许1个用户配置文件和注册/登录流程?我需要2个独立的网站吗?
我已经阅读了我想要做的是创建一个多租户应用程序,但我还没有看到asp.net mvc的任何一个很好的例子。理想情况下,我希望用户转到www.foo.com并消费" stuff"销售用户在www.foo.com/Sales上创建。用户将在www.foo.com注册并拥有自己的流程,用户将在www.foo.com/Sales注册并拥有自己的产品创建流程。
首先使用EF代码和ASP.net标识对此进行建模的最佳方法是什么?
我的想法:
答案 0 :(得分:0)
出于您的目的,您应使用自定义成员资格提供程序。这是关于它的文章的链接http://logcorner.wordpress.com/2013/08/29/how-to-configure-custom-membership-and-role-provider-using-asp-net-mvc4/。 关于如何创建自定义成员资格提供程序
,还有How do I create a custom membership provider for ASP.NET MVC 2?答案 1 :(得分:0)
自定义成员资格提供程序的示例(请注意这是我的旧代码,因此您应该尝试对其进行优化):
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}}表,其中包含1到N的关系(在Role
表中使用foreign key - Id_role
)< / p>