asp.net-mvc – MVC授权 – 多个登录页面
发布时间:2020-12-16 04:16:00 所属栏目:asp.Net 来源:网络整理
导读:我在MVC控制器中有以下方法,当用户未登录时,该方法重定向到登录页面. [Authorize]public ActionResult Search() { return View();}[Authorize]public ActionResult Edit() { return View();} 是否有一种快速/简单/标准的方法将第二个操作重定向到除web.confi
我在MVC控制器中有以下方法,当用户未登录时,该方法重定向到登录页面.
[Authorize] public ActionResult Search() { return View(); } [Authorize] public ActionResult Edit() { return View(); } 是否有一种快速/简单/标准的方法将第二个操作重定向到除web.config文件中定义的页面之外的其他登录页面? 或者我必须做些什么 public ActionResult Edit() { if (IsUserLoggedIn) return View(); else return ReturnRedirect("/Login2"); } 解决方法
我认为可以通过创建自定义授权过滤器:
public class CustomAuthorization : AuthorizeAttribute { public string LoginPage { get; set; } public override void OnAuthorization(AuthorizationContext filterContext) { if (!filterContext.HttpContext.User.Identity.IsAuthenticated) { filterContext.HttpContext.Response.Redirect(LoginPage); } base.OnAuthorization(filterContext); } } 在你的行动中: [CustomAuthorization(LoginPage="~/Home/Login1")] public ActionResult Search() { return View(); } [CustomAuthorization(LoginPage="~/Home/Login2")] public ActionResult Edit() { return View(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET ModalPopupExtender单击“事件”
- 如何使用asp .net web api,实体框架和json(代码优先)来关联
- ASP.Net/C#将NameValueCollection转换为IDictionary?
- .net – ELMAH对企业库异常处理块
- asp.net-mvc – ASP.NET MVC中具有动态数量的texbox的表单
- asp.net-mvc – 404处理Azure网站
- 在MVC 4中使用ASP.Net Identity
- asp:RadioButton和javascript函数$find()
- 如何通过asp.net mvc或jquery提交包含动态数据行的表?
- ASP.NET框架中的异步页面 – 其他线程在哪里,如何重新连接?
推荐文章
站长推荐
- asp.net-mvc-2 – 如何使用控制器的模型提供ASP.
- views – 我应该在asp.net MVC6中为我的所有图像
- asp.net – 在VMware主机中扩展 – 添加vCPU或VM
- asp.net-web-api – 将List转换为JSon并在Web AP
- asp.net – 哪个最快?数据检索
- asp.net-mvc – 如何使用Autofac将属性“绑定”到
- ASP.NET Web API项目自定义接口路由和HTTP请求方
- 使用Linq表达式在扩展方法中有趣(?)
- asp.net – MaintainScrollPositionOnPostback属
- asp.net-mvc – ASP .Net MVC 3:单元测试控制器
热点阅读