asp.net-mvc-3 – ASP.Net MVC 3重定向未经授权的用户不登录Url
发布时间:2020-05-23 19:09:15 所属栏目:asp.Net 来源:互联网
导读:我有一个使用ASP.Net MVC3并使用角色成员的项目。我在每个控制器中使用授权。 例如: [Authorize(Roles = Administrator)] public ActionResult Index(string q, int i) { return View(model); } 如果有人没有管理员的角色,那么默认情况下将重定向到登录页面
|
我有一个使用ASP.Net MVC3并使用角色成员的项目。我在每个控制器中使用授权。
[Authorize(Roles = "Administrator")]
public ActionResult Index(string q,int i)
{
return View(model);
}
如果有人没有管理员的角色,那么默认情况下将重定向到登录页面。如何更改它,所以它会重定向到Views / Shared / UnAuthorize.cshtml?或者如果有人没有管理员的角色,它会显示消息框(警报)? 提前致谢。 解决方法只需更改必须在web.config中显示的页面(检查路由是否存在)<authentication mode="Forms"> <forms loginUrl="~/UnAuthorize" timeout="2880" /> </authentication> 相反,如果您要为每个角色重定向到特定的路径,则可以使用自己的方式来扩展AuthorizeAttribute。这样的东西(没有测试,我写这个给你一个想法) public class CheckAuthorize : ActionFilterAttribute
{
public Roles[] Roles { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//Your code to get the user
var user = ((ControllerBase)filterContext.Controller).GetUser();
if (user != null)
{
foreach (Role role in Roles)
{
if (role == user.Role)
return;
}
}
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
if user.Role==Role.Administrator
{
redirectTargetDictionary.Add("action","Unauthorized");
redirectTargetDictionary.Add("controller","Home");
}
else
{
redirectTargetDictionary.Add("action","Logon");
redirectTargetDictionary.Add("controller","Home");
}
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- .net – 在项目之间共享MasterPages的最佳方法是什么?
- ASP.NET WebMethod返回包含在引号中的JSON
- asp.net-mvc-3 – 如何在输出缓存中使用动态持续时间值?
- asp.net-core – 如何在EF Core 2.1.0中为Admin用户播种?
- asp.net-mvc – 当我不知道内容类型时如何返回文件结果
- asp.net-mvc – 在MVC中将值从Controller传输到Shared View
- asp.net – 当我禁用提交按钮以防止双击时,为什么我的表单没
- asp.net-core – 从ActionFilterAttribute设置ViewBag
- asp.net – 检查表单是否有密钥?
- 在asp.net后面的代码中创建javaScript变量
推荐文章
站长推荐
- asp.net-mvc – 将隐藏文本字段附加到表单MVC
- asp.net – “此操作需要IIS集成管道模式
- asp.net 4.0:是否有相当于ClientIDMode的INPUT的
- asp.net-mvc – HtmlHelper NameFor方法
- .net – .ToTitleCase不适用于所有大写字符串
- asp.net-core – 重置实体框架7迁移
- 如何在ASP.Net MVC2中完成此类URL?
- ASP.NET UserControl不初始化子控件
- asp.net – Server.Transfer vs. Context.Rewrit
- asp.net-mvc – Firefox在Ajax请求重定向期间不保
热点阅读
