MVC4 SimpleMemberhip Intranet的Web应用具有自定义角色 [英] MVC4 SimpleMemberhip Intranet webapp with Custom Roles
问题描述
我使用SimpleMembership与WebMatrix中。自公司内部网的web应用,我现在用的是exisitng域用户在自定义角色组合,并将其存储在本地webpages_表。我想开发类来管理用户和放大器;角色。或许我会对此错误的方式,但这里是我有以下我在哪里卡住了。
I am using SimpleMembership with WebMatrix. Since its an Intranet webapp, I am using the exisitng domain users in combination with custom roles and storing them in local webpages_ tables. I am trying to develop classes to manage the users & roles. Perhaps I am going about this the wrong way, but here is what I have and below where I am stuck.
在Global.asa中设置此
Setting this in global.asa
WebSecurity.InitializeDatabaseConnection("SqlRoleManagerConnection", "webpages_Users", "UserID", "Username", false);
在web.config中设置此(其它来源称添加roleManager = TRUE部分,但它目前是没有它)
Setting this in web.config (other sources said to add roleManager=true section but it currently works without it)
<!--<roleManager enabled="true" defaultProvider="SqlRoleManager">
<providers>
<clear />
<add name="SqlRoleManager" type="System.Web.Security.SqlRoleProvider" connectionStringName="SqlRoleManagerConnection" applicationName="YourAppName" />
</providers>
</roleManager>-->
<httpRuntime targetFramework="4.5" />
<authentication mode="Windows" />
<authorization>
<allow roles="Managers" />
<allow users="?" />
</authorization>
数据访问类(使用控制器)
Data Access class (used by controllers)
public class Membership
{
private OFACDB _db = new OFACDB();
public string UserID { get; set; }
public string UserName { get; set; }
public string RoleName { get; set; }
public string Name { get; set; }
public const string Domain = "LAN\\";
public void Delete()
{
Roles.RemoveUserFromRole(this.UserName, this.RoleName);
}
public void AddMemberToRole()
{
if (!Roles.IsUserInRole(Membership.Domain + this.UserName, this.RoleName))
Roles.AddUserToRole(Membership.Domain + this.UserName, this.RoleName);
}
public void AddMember()
{
webpages_Users member = new webpages_Users();
member.Username = Membership.Domain + this.UserName;
_db.webpages_Users.Add(member);
_db.SaveChanges();
}
public void DelMember(string id)
{
webpages_Users member = _db.webpages_Users.Find(id);
_db.webpages_Users.Remove(member);
_db.SaveChanges();
}
}
public class MembershipViewModel : List<Membership>
{
private OFACDB _db = new OFACDB();
//public List<webpages_Users> UserView { get; set; }
public IQueryable<webpages_Users> GetAllRecords()
{
var view = _db.webpages_Users
.OrderBy(v => v.Username);
return view;
}
public void GetAllRoleUsers(string role) //Get application's users
{
if (Roles.RoleExists(role))
{
foreach (var item in Roles.GetUsersInRole(role))
{
var user = new Membership();
user.UserName = item;
user.Name = item;
user.RoleName = role;
this.Add(user);
}
}
}
public void GetNetworkUsers() //Get Network Users (AD)
{
var domainContext = new PrincipalContext(ContextType.Domain);
var groupPrincipal = GroupPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, "Domain Users");
foreach (var item in groupPrincipal.Members)
{
var user = new Membership();
user.UserName = item.SamAccountName;
user.Name = item.Name;
this.Add(user);
}
}
}
和角色由控制器控制访问
And controller controls access by roles
[Authorize(Roles = "Admins")]
public ActionResult Index()
{
var users = new MembershipViewModel();
users.GetAllRoleUsers("Managers");
return View(users);
}
建议?
我用Roles.GetUsersInRole,列出用户中的一个角色,但我不能删除它们很容易,因为这调用不返回用户ID,如果我使用用户名查找/删除记录,然后它在URL,因为逃脱用户名包含域\\字符。
ADVICE? I use Roles.GetUsersInRole to list out users in a role, but I can't delete them very easily as this call does not return UserIDs and if I use the username to find/delete record, then it is escaped in the URL because the usernames contain the domain\ characters.
/帐号/删除/ LAN%5CLAN%5Ctest
/Account/Delete/LAN%5CLAN%5Ctest
寻找请教也许采取了不同的方法,以这些类是否有人这样做过。我是否需要使用一个成员资格提供程序和角色提供?
Looking for advice on perhaps taking a different approach to these classes if anyone else has done this before. Do i need to use a Membership Provider and Role Provider?
推荐答案
我想只作评论,但我不能,因为我只有一个卑微的44点声望。
I wanted to make a comment only but I couldn't because I only have a lowly 44 points rep.
我知道这是旧的,但我一直在寻找同样的事情,并希望加入到上面有他自己的DB与@Pabloker它使用内建的DB的@Vic之间的意见。我想asp.net都有自己的脚本创建这个数据库,并在此解释的博客
I know this is old but I was looking for the same thing and wanted to add to the comments above between the @Vic which has his own DB vs. @Pabloker which uses the builtin DB. I guess asp.net has its own script in creating this database and is explained in this blog
cd \Windows\Microsoft.NET\Framework64\v4.0.30319
.\aspnet_regsql -C "Data Source=localhost;Database=ACME.Config;Integrated Security=True;" -A r
这篇关于MVC4 SimpleMemberhip Intranet的Web应用具有自定义角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!