你应该使用Windows身份验证,使用的IPrincipal,你将有一个用户对象,你可以问IsInRole特定的基于角色的安全性,而不是位/布尔
阅读所有关于它在Asp.net windows authentication
以及如何实现的IPrincipal Implement custom security
代码示例: 用户对象:
public class User : IPrincipal
{
private readonly IPrincipal _user;
public IIdentity Identity { get; private set; }
public User (IPrincipal user)
{
Identity = user.Identity;
_user = user;
}
public bool IsInRole(string role)
{
return _user.IsInRole(role);
}
}
在MVC添加过滤器
public class CustomAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter
{
public void OnAuthentication(AuthenticationContext filterContext)
{
var user= new User (HttpContext.Current.User);
Thread.CurrentPrincipal = user;
}
}
而且使用
filters.Add(new CustomAuthenticationAttribute());
然后,用你的应用程序
var user = new User(Thread.CurrentPrincipal);
if(user.IsInRole("admin")) /* do the choka choka */;
我有这个想法内的用户对象时过滤器添加到您的一个FilterConfig你确实是对的,而这正是我需要的。然而,我现在一直在考虑最近两个小时,而且我不知道如何实现这一点。你会有我的代码示例吗?主要关于如何获取用户对象以及认证码的放置位置。 –
为您添加了一个代码示例 – Thorarins
非常感谢。我做了一个编辑建议,其中包含我必须使其适应的适应性。这对于像我这样的未来新手来说,他们不知道什么是什么。 –