Bil*_*lla 4 c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4
我有以下要求来实现访问控制列表
public class SecurityObject{
public string Key{get;set;}
public string DisplayName{get;set;}
public bool isAllowed{get;set;}
}
public class Role{
List<SecurityObject> AccessibleObjects{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
目前我使用表单身份验证进行基本授权.以下是我的代码
的Global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
public override void Init()
{
this.PostAuthenticateRequest += new
EventHandler(MvcApplication_PostAuthenticateRequest);
base.Init();
}
void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
HttpCookie authCookie =
HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
string encTicket = authCookie.Value;
if (!String.IsNullOrEmpty(encTicket))
{
FormsAuthenticationTicket ticket =
FormsAuthentication.Decrypt(encTicket);
string[] userData = ticket.UserData.Split(new string[] { "___" },
StringSplitOptions.None);
string[] roles = null;
if (userData.Length > 1)
{
roles = userData[1].Split(',');
}
MyCustomIdentity identity = new MyCustomIdentity(ticket);
GenericPrincipal principle = new GenericPrincipal(identity, roles);
HttpContext.Current.User = principle;
}
}
}}
Run Code Online (Sandbox Code Playgroud)
我目前的控制器类
public class AdminController : Controller
{
[HttpPost, Authorize, ValidateAntiForgeryToken]
public ActionResult SaveUser(UserDetailViewModel viewModel)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标控制器类
public class AdminController : Controller
{
[HttpPost, Authorize(ACLKey="USR_SAVE"), ValidateAntiForgeryToken]
public ActionResult SaveUser(UserDetailViewModel viewModel)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我希望我的action方法用ACLKey修饰,我想检查用户角色是否具有给定的密钥,并根据我需要执行或返回HttpUnauthorizedResult页面,即使是来自jQuery的Ajax请求.
我在ASP.NET MVC中提到了许多像自定义授权但我没有找到执行表单身份验证和自定义ACLKey检查的方法.
如何USR_SAVE使用 CustomAuthorizeFilter 解析值并处理自定义身份验证?
你可以这样试试
public class FeatureAuthenticationAttribute : FilterAttribute, IAuthorizationFilter
{
public string AllowFeature { get; set; }
public void OnAuthorization(AuthorizationContext filterContext)
{
var filterAttribute = filterContext.ActionDescriptor.GetFilterAttributes(true)
.Where(a => a.GetType() ==
typeof(FeatureAuthenticationAttribute));
if (filterAttribute != null)
{
foreach (FeatureAuthenticationAttribute attr in filterAttribute)
{
AllowFeature = attr.AllowFeature;
}
List<Role> roles =
((User)filterContext.HttpContext.Session["CurrentUser"]).Roles;
bool allowed = SecurityHelper.IsAccessible(AllowFeature, roles);
if (!allowed)
{
filterContext.Result = new HttpUnauthorizedResult();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在你的行动方法
[FeatureAuthentication(AllowFeature="USR_SAVE")]
public ActionResult Index()
{
}
Run Code Online (Sandbox Code Playgroud)
希望对你有帮助!
您可以使用过滤器属性:
public class ACLCheckAttribute : FilterAttribute, IActionFilter
Run Code Online (Sandbox Code Playgroud)
在OnActionExecuting中,您可以获取USR_SAVE。在不知道它来自哪里的情况下,我会假设它来自:
如果有其他地方,请评论在哪里。您可以将此属性应用于控制器或方法,或者通过将其添加到 globalfilters 集合 (GlobalFilters.Filters.Add()) 或 App_Start 文件夹中的 FilterConfig 文件中来全局设置它。
| 归档时间: |
|
| 查看次数: |
5749 次 |
| 最近记录: |