ASP.Net MVC 3 – 密码保护视图
发布时间:2020-12-16 07:35:02 所属栏目:asp.Net 来源:网络整理
导读:Visual Studio 2010 – MVC 3 我有一个asp.net mvc应用程序的管理部分,我想限制访问.应用程序不会使用帐户,因此我不会使用管理员角色或用户来授权访问权限. 我希望通过输入单个密码来访问该部分.本节将介绍一些操作.我已经设置了一个管理控制器,它可以重定向
Visual Studio 2010 – MVC 3
我有一个asp.net mvc应用程序的管理部分,我想限制访问.应用程序不会使用帐户,因此我不会使用管理员角色或用户来授权访问权限. 我希望通过输入单个密码来访问该部分.本节将介绍一些操作.我已经设置了一个管理控制器,它可以重定向到许多不同的视图,因此基本上任何需要限制该控制器控制的视图. 我也希望它只需要为会话输入一次密码,因此当浏览器关闭并重新打开时,需要重新输入密码. 我怎么做到这一点? 解决方法
假设您有一个名为Protected的View文件夹(作为您的控制器),并且您有几个指向多个Views的Actions,我会这样做:
>使用动作过滤器装饰控制器/动作,例如:[SimpleMembership] 在代码中: public class SimpleMembershipAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { //redirect if not authenticated if (filterContext.HttpContext.Session["myApp-Authentication"] == null || filterContext.HttpContext.Session["myApp-Authentication"] != "123") { //use the current url for the redirect string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath; //send them off to the login page string redirectUrl = string.Format("?ReturnUrl={0}",redirectOnSuccess); string loginUrl = "/Protected/SignIn" + redirectUrl; filterContext.HttpContext.Response.Redirect(loginUrl,true); } } } 和你的控制器 public class ProtectedController : Controller { [SimpleMembership] public ActionResult Index() { return View(); } public ActionResult SignIn() { return View(); } [HttpPost] public ActionResult SignIn(string pwd) { if (pwd == "123") { Session["myApp-Authentication"] = "123"; return RedirectToAction("Index"); } return View(); } } 如果你想装饰整个控制器,你需要将SignIn方法移到外面以便到达那里,你需要进行身份验证. 源代码: 您可以下载简单的MVC3解决方案http://cl.ly/JN6B或免费查看GitHub上的代码. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 如何在所选项目上启用工作流状态“写入”?
- asp.net – 单声道随机CS0006编译错误w / fastcgi-mono-ser
- asp.net – 在Internet Explorer 8中使用ScriptManager.Reg
- asp.net – Response.Redirect和线程被中止错误?
- 在Azure中设置ASP.NET 5 Web应用程序的SQL连接字符串
- asp.net-mvc – 直接使用域模型的视图是否危险?
- asp.net – CKEditor MVC 3实现
- 如何在ASP.NET MVC中记录未处理的异常?
- asp.net-mvc-3 – 在asp.net MVC 3中使用WIF,我在哪里定义S
- asp.net-mvc – DataAnnotation验证和自定义ModelBinder
推荐文章
站长推荐
- asp.net – Gridview编辑,点击两次问题
- asp.net-mvc – ASP.NET MVC – 当参数为null时绑
- EF Core 数据变更自动审计设计
- asp.net-mvc – MVC.net获取枚举显示名称而不必参
- asp.net – 在构建Web应用程序后使用visual stud
- asp.net – Ajax上的Identity Server 3 – 401而
- asp.net-mvc – 如何修复System.Net.Sockets.Soc
- asp.net-mvc – @Model和@model之间的区别
- asp.net-mvc-3 – 如何在带有剃刀的asp.net mvc3
- asp.net-mvc – 在链接文本中使用HTML元素创建一
热点阅读