小编zyb*_*oxz的帖子

HttpActionContext.Request没有CreateResponse Meth

我正在尝试在ASP.Net Web API中创建自定义AuthorizeAttribute来处理Basic Auth.当重写HandleUnauthorizedRequest时,我发现HttpActionContext.Request没有CreateResponse方法.

该项目是MVC 4目标.net 4.5.我使用nuget将Web API更新到版本2.


using System;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Web.Http;

namespace BasicAuth.Security
{
    public class BasicAuthAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                return;
            }

            var authHeader = actionContext.Request.Headers.Authorization;
            if (authHeader != null)
            {
                if (authHeader.Scheme.Equals("basic", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(authHeader.Parameter))
                {
                    var credentials = GetCredentials(authHeader);

                    //Handle authentication

                    return;
                }
            }

          HandleUnauthorizedRequest(actionContext);
        }

        private string[] GetCredentials(AuthenticationHeaderValue authHeader)
        {
            var raw = authHeader.Parameter;
            var encoding = Encoding.ASCII;
            var credentials = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-web-api

29
推荐指数
1
解决办法
2万
查看次数

标签 统计

asp.net-mvc ×1

asp.net-web-api ×1

c# ×1