我在編程方面頗爲新穎,我的英語很糟糕,所以我很抱歉如果這篇文章讓你感到困惑。基於MVC項目的角色
我正在實施一個基本的ASP .NET MVC Web應用程序,它將管理公司的信息。 在這個應用程序中有兩個頁面。 一個用於查看信息,無需任何訪問此頁面的信息。 另一個用於編輯您可以訪問此頁面的信息,只要您擁有公司的管理員角色。
public ActionResult ViewInfo(string companyId)
{
return View();
}
[Authorize(Roles = "Admin")]
public ActionResult EditInfo(string companyId)
{
return View();
}
如果我有可以訪問到2家公司的帳戶,但該帳戶在這兩個公司的完全不同的角色。 (例如,帳戶是公司A的管理員和公司B的普通用戶)。
是否有任何簡單的方法可以根據我將要訪問的公司獲取角色?我試過使用自定義RoleProvider,但似乎我無法將其他參數傳遞給該方法。
有點像這樣?
public class CustomRoleProvider : RoleProvider
{
public override bool IsUserInRole(string username, string roleName)
{
string companyId = GetCompanyId(); // Is there any good way to get this companyId from the Controller?
return userCompanyRoles.Any(u => u.Username == username && u.CompanyId == companyId && u.Roles.Any(r => r.Name == roleName));
}
...
}
編輯
車型有4款在我的應用程序:
public class User {
public string Id { get; set;}
...
}
public class Company {
public string Id { get; set;}
...
}
public class Role {
public string Name { get; set;}
...
}
public class UserCompanyRoles {
public string UserId { get; set; }
public string CompanyId { get; set; }
public List<string> Roles { get; set; }
}
所以表看起來像這樣
UserId CompanyId Role
1 A User
1 A Admin
1 B User
2 B Admin
2 C User
3 A Admin
3 C Admin
你還會預計公頃超過2家公司? –
@ErikPhilips當然!而且不同的角色也會有更多的頁面。 – BPRW