微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc-3 – 用于基本身份验证的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)我有什么问题吗?

谢谢.

-缺口

解决方法

1)ActionFilterattribute是最好的方法吗?
我想是这样.此方法反映了内置Authorize属性的实现.

2)设置filterContext.Result是否正确拒绝访问控制器方法
是.多数民众赞成在那里. (1)

3)我有什么问题吗?

>您假设Authorization标头的内容位于
格式正确,编码正确.
>您认为该请求是用于基本身份验证而不是任何请求
其他认证方案.
>我更愿意使用HttpUnauthorizedResult()发送一个http
通过HttpNotFoundResult()发生401错误而不是http 404错误.

下面是我的代码实现(我肯定也有它的问题).

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

原文地址:https://www.jb51.cc/aspnet/251917.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐