asp.net-mvc-3 – 用于基本身份验证的asp mvc 3 ActionFilter
发布时间:2020-12-16 00:17:50 所属栏目:asp.Net 来源:网络整理
导读:我有一个使用基本身份验证的ASP MVC3 restful服务.搜索堆栈溢出后,我创建了以下代码. public class BasicAuthentication : ActionFilterAttribute{ public override void OnActionExecuting(ActionExecutingContext filterContext) { var req = filterContex
我有一个使用基本身份验证的ASP MVC3 restful服务.搜索堆栈溢出后,我创建了以下代码.
public class BasicAuthentication : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var req = filterContext.HttpContext.Request; if (String.IsNullOrEmpty(req.Headers["Authorization"])) { filterContext.Result = new HttpNotFoundResult(); } else { var credentials = System.Text.ASCIIEncoding.ASCII .GetString(Convert.FromBase64String(req.Headers["Authorization"].Substring(6))) .Split(':'); var user = new { Name = credentials[0],Password = credentials[1] }; if(!(user.Name == "username" && user.Password == "passwords")) { filterContext.Result = new HttpNotFoundResult(); } } } } 1)ActionFilterAttribute是最好的方法吗? 2)设置filterContext.Result是否正确拒绝访问控制器方法? 3)我有什么问题吗? 谢谢. -缺口 解决方法
1)ActionFilterAttribute是最好的方法吗?
我想是这样.此方法反映了内置Authorize属性的实现. 2)设置filterContext.Result是否正确拒绝访问控制器方法? 3)我有什么问题吗? >您假设Authorization标头的内容位于 下面是我的代码实现(我肯定也有它的问题). public override void OnActionExecuting(ActionExecutingContext filterContext) { try { if (String.IsNullOrEmpty(filterContext.HttpContext.Request.Headers["Authorization"])) { filterContext.Result = new HttpUnauthorizedResult(); } else { if (filterContext.HttpContext.Request.Headers["Authorization"].StartsWith("Basic ",StringComparison.InvariantCultureIgnoreCase)) { string[] credentials = ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(filterContext.HttpContext.Request.Headers["Authorization"].Substring(6))).Split(':'); if (credentials.Length == 2) { if (String.IsNullOrEmpty(credentials[0])) { filterContext.Result = new HttpUnauthorizedResult(); } else if (!(credentials[0] == "username" && credentials[1] == "passwords")) { filterContext.Result = new HttpUnauthorizedResult(); } } else { filterContext.Result = new HttpUnauthorizedResult(); } } else { filterContext.Result = new HttpUnauthorizedResult(); } } base.OnActionExecuting(filterContext); } catch { filterContext.Result = new HttpUnauthorizedResult(); } } 笔记 >我没有包含用户名和密码的非法字符检查. 参考 (1)http://msdn.microsoft.com/en-us/magazine/gg232768.aspx (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – MemoryCache对象和负载均衡
- 在ASP.NET MVC中使用MySQL的AccountController
- asp.net – Sitecore – 以编程方式清除用户的缓存
- asp.net-mvc – Robots.txt,禁止多语言URL
- ASP.NET性能优化之局部缓存分析
- asp.net-mvc – 神秘的ASP.NET MVC Action高延迟问题?
- C#_.net core 3.0自定义读取.csv文件数据_解决首行不是标题
- asp.net-mvc – 防止JsonResult自动格式化日期
- asp.net-mvc – 为什么我在带有godaddy服务器的MVC3应用程序
- asp.net-mvc – 某些Razor视图没有发布
推荐文章
站长推荐
- asp.net-mvc – 在MVC 3项目中使用T4MVC生成的代
- ASP.NET主题图像
- ASP.NET SiteMap – 有没有办法以编程方式查看它
- asp.net – 在Global.asax上使用IHttpModule
- asp.net – 不引用MVC的AllowHtml属性
- ASP.NET UpdatePanel库引用错误
- asp.net – IE6 vs IE8,按钮vs超链接,CSS渲染问题
- asp.net-mvc – DAL中的app.config和WebApplicat
- asp.net-mvc – 添加附加参数以表单提交
- asp.net – 有没有办法使用System.Net.Mail.Send
热点阅读