经过大量谷歌搜索并阅读了有关如何在 ASP.NET 应用程序中管理混合模式身份验证的几种解决方案后,我仍然没有适合我的问题的解决方案。
我必须为一堆不同的用户组实现一个 Intranet 应用程序。到目前为止,我一直使用 Windows 身份验证,它的实现非常简单。当涉及到授权用户组使用特殊应用程序功能时,我遇到了问题。
Using [Authorize(Users = "DOMAIN\\USER")]
效果很好,但由于我无法访问活动目录管理,我无法按照应用程序所需的方式配置角色管理。
除了在活动目录中定义的角色和成员资格之外,我想做的是定义自定义角色和成员资格(这样的扩展可能吗?例如,通过实现自己的membershipprovider?)。
您认为解决我的问题的最佳方案是什么?除了 Windows 身份验证之外,我是否真的必须使用表单身份验证来实现复杂的混合模式身份验证?
使用的技术:
- 微软 SQL Server 2008
- 女士VS 2010
- ASP.NET MVC 3 - Razor 视图引擎
- ASP.NET MVC 的 Telerik 扩展
- Windows Server 2008 上的 IIS 7
编辑(最终解决方案感谢 dougajmcdonald 的帮助):
在指示我使用自定义 IPrincipal 实现后,我找到了一些解决方案here http://smehrozalam.wordpress.com/2009/01/01/using-customprincipal-with-forms-authentication-in-aspnet/ and here http://jbaurle.wordpress.com/2007/07/24/creating-custom-windows-authentication-roles-in-asp-net-2/。将所有内容放在一起,我得出以下解决方案:
1.创建自定义主体实现:
public class MyPrincipal: WindowsPrincipal
{
List<string> _roles;
public MyPrincipal(WindowsIdentity identity) : base(identity) {
// fill roles with a sample string just to test if it works
_roles = new List<string>{"someTestRole"};
// TODO: Get roles for the identity out of a custom DB table
}
public override bool IsInRole(string role)
{
if (base.IsInRole(role) || _roles.Contains(role))
{
return true;
}
else
{
return false;
}
}
}
2.通过扩展“Global.asax.cs”文件将我的自定义主体实现集成到应用程序中:
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (Request.IsAuthenticated)
{
WindowsIdentity wi = (WindowsIdentity)HttpContext.Current.User.Identity;
MyPrincipal mp = new MyPrincipal(wi);
HttpContext.Current.User = mp;
}
}
3.在我的应用程序中使用我的自定义角色进行授权
public class HomeController : Controller
{
[Authorize(Roles= "someTestRole")]
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
}
有用!!!是的!