asp mvc 3 ActionFilter 用于基本身份验证
我有一个使用基本身份验证的 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)我做错了什么吗?
谢谢。
-缺口
I have an ASP MVC3 restful service that uses basic authentication. After searching stack overflow, I created the following code.
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) Is an ActionFilterAttribute the best way to do this?
2) Is setting filterContext.Result the correct way to deny access to the controller method?
3) Is there anything I'm doing wrong?
Thanks.
-Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
1) ActionFilterAttribute 是实现此目的的最佳方法吗?
我想是的。此方法反映了内置
Authorize
属性的实现。2) 设置
filterContext.Result
是拒绝访问控制器方法的正确方法吗?是的。这就是它的用途。 (1)
3) 我做错了什么吗?
格式正确且编码正确。
其他认证方案。
通过 HttpNotFoundResult() 生成 401 错误,而不是 http 404 错误。
下面是我对代码的实现(我确信也有问题)。
注释
参考
(1) http://msdn.microsoft. com/en-us/magazine/gg232768.aspx
1) Is an
ActionFilterAttribute
the best way to do this?I think so. This approach mirrors the implementation of the built in
Authorize
attribute.2) Is setting
filterContext.Result
the correct way to deny access to the controller method?Yes. Thats whats it there for. (1)
3) Is there anything I'm doing wrong?
correct format and is correctly encoded.
other authentication scheme.
HttpUnauthorizedResult()
to send a http401 error instead of a http 404 error via
HttpNotFoundResult()
.Below in my implementation of your code (which I'm sure has its issues too).
Notes
References
(1) http://msdn.microsoft.com/en-us/magazine/gg232768.aspx
Adrian 的重构版本
Refactored version of Adrian's
这是基本身份验证的官方示例:
http://www.asp .net/web-api/overview/security/authentication-filters
另一篇文章,现在使用 OWIN:
https:// lbadri.wordpress.com/2013/07/13/basic-authentication-with-asp-net-web-api-using-owin-middleware/
Here's a sort of official sample for basic authentication:
http://www.asp.net/web-api/overview/security/authentication-filters
Another article, now using OWIN:
https://lbadri.wordpress.com/2013/07/13/basic-authentication-with-asp-net-web-api-using-owin-middleware/
1) 不,ActionFilter 属性不是验证用户身份的好方法。
(由于我们需要验证一次并设置验证cookie,因此HttpContext.User将保持验证状态直到cookie过期)
2)是的,设置filtercontext.Result是防止访问的理想方法。 (但不是分配 HttpNotFoundResult,而是使用 RedirectResult 重定向到登录页面)
3)我真的不明白为什么要对授权进行这样的实现。
最好的方法是执行一个操作来接收表单发布的数据(用户名和密码)。并使用Authorize属性来防止未经授权的访问。
以下是VS2010中默认MVC3示例应用程序的代码。
1) No, ActionFilter attributes are not a good approach to authenticate a user.
(As we need to authenticate once and set authenticate cookie, so HttpContext.User will remain authenticated till cookie expires)
2) Yes, setting filtercontext.Result is a ideal way to prevent access. ( But Instead of assigning HttpNotFoundResult, use RedirectResult to redirect to login page)
3) I really don't understand why to have such implementation for Authorization.
The best approach would be to have an action that will receive form posted data (username and password). and use Authorize attribute to prevent unauthorize access.
following is the code from default MVC3 sample application in VS2010.