使用ASP.net Identity禁用基于角色授权的MVC视图中的输入字段
Disable Input Fields in MVC View on Role Based Authorization using ASP.net Identity
我已经在MVC应用程序中重写了AuthorizeAttribute类,用于基于角色的授权。
[HttpPOST]
[CustomAuthorize(Roles = "AddCOA")]
public ActionResult Edit([Bind(Include = "N100,S104,S103,S101,S1,S100,D1")] TrM trM)
{
if (ModelState.IsValid)
{
db.Entry(trM).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("View",trM);
}
return View(trM);
}
我从视图使用凭证列表调用这个控制器方法。现在我必须禁用编辑ActionLink按钮在视图中的某个角色,我怎么能实现这一点?
@Html.Actionlink("Edit", "Edit", "Controller", new{@class = "btn btn-success"})
现在它会自动重定向到登录页面
可以使用razor检查当前用户是否在指定的角色中:
@if (User.IsInRole("AddCOA"))
{
@Html.Actionlink("Edit", "Edit", "Controller", new { @class = "btn btn-success" })
}
else
{
@Html.Actionlink("Edit", "Edit", "Controller", new { @class = "btn btn-success disbled" })
}
方式一:
你可以在服务器端使用自定义的ActionLink扩展来处理它,该扩展检查是否显示编辑链接到基于角色的用户:
public static class LinkExtensions
{
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName)
{
return htmlHelper.ActionLinkAuthorized(linkText, actionName, null, new RouteValueDictionary(), new RouteValueDictionary());
}
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues)
{
return htmlHelper.ActionLinkAuthorized(linkText, actionName, null, new RouteValueDictionary(routeValues), new RouteValueDictionary());
}
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
{
return htmlHelper.ActionLinkAuthorized(linkText, actionName, controllerName, new RouteValueDictionary(), new RouteValueDictionary());
}
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues)
{
return htmlHelper.ActionLinkAuthorized(linkText, actionName, null, routeValues, new RouteValueDictionary());
}
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
{
return htmlHelper.ActionLinkAuthorized(linkText, actionName, null, new RouteValueDictionary(routeValues), new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
return htmlHelper.ActionLinkAuthorized(linkText, actionName, null, routeValues, htmlAttributes);
}
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
return htmlHelper.ActionLinkAuthorized(linkText, actionName, controllerName, new RouteValueDictionary(routeValues), new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
if (UserInRole()) // your business logic here for role check
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
}
return MvcHtmlString.Empty;
}
}
并在View中使用:
@Html.ActionLinkAuthorized("Edit", "Edit", "Controller", new{@class = "btn btn-success"})
方式2:
您可以修改自定义属性代码以重定向到显示用户未经授权查看该页的页面:
public class AuthorizationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string actionName = filterContext.ActionDescriptor.ActionName;
string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
if (!AllowedToAccess()) // if not in specific role show page with message that user is unauthorized to view this page
{
string redirectUrl = string.Format("?returnUrl={0}", filterContext.HttpContext.Request.Url.PathAndQuery);
filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl + redirectUrl, true);
}
else
{
base.OnActionExecuting(filterContext); if authorized user allow it to view
}
}
和在Web。配置url,当用户不在角色中时调用该操作:
<authentication mode="Forms">
<forms loginUrl="~/UnAuthorized" timeout="2880" />
</authentication>
相关文章:
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- 将Javascript数组发送到控制器ASP.NET MVC
- 如何使用Spring MVC将Facebook返回的响应数据保存在Java类中
- 如何在MVC中使用jQuery在文本框旁边显示文本
- MVC 3页面导航和使用javascript传递参数
- 在mvc应用程序中,在回发时保留最初隐藏的文本框的隐藏或可见状态
- Flash Uploadify在调用我的MVC控制器时没有保留会话/授权
- 将不在模型中的数据返回到mvc控制器
- MVC在通过javascript提交时会丢失值
- 使用javascript在MVC中查找网格长度时出错
- 什么'是在asp.net MVC中将本地化的resources.resx文件转换为javascript文件的有效
- 在MVC 4中,对Controller的Ajax调用为空
- C#asp.net mvc Set CheckBoxFor已检查的具有Model Value的属性
- 无法在mvc视图中使用ajax调用获取操作返回的模型对象列表
- ASP.NET MVC 3-在ajax调用后,重定向到新页面或生成页面刷新
- 使用mvc和jquery显示更多点击数据
- 在Asp,NET MVC中使用Javascript访问受保护的页面返回未经授权的结果
- ajax后调用MVC动作时,我使用[授权]给出状态:200错误
- 当与Angularjs混合使用时,MVC授权不起作用
- 使用ASP.net Identity禁用基于角色授权的MVC视图中的输入字段