【问题标题】:Call web api with basic authentication always get 401 unauthorized from IIS使用基本身份验证调用 web api 总是从 IIS 获得 401 未授权
【发布时间】:2016-12-14 18:53:19
【问题描述】:

今晚我来搜索一些关于如何调用托管在 IIS 中的 web api 的帮助。

在本地,从 Visual Studio 到 iis express,一切正常。但奇怪的是,在我的 IIS 服务器上发布后。我总是得到未经授权的 401 :'(

这是我使用的代码和来自我的 IIS 服务器的设置。如果有人可以帮助我,我将不胜感激。 谢谢

**

我尝试调用的控制器和函数(具有基本身份验证属性)

**

    [HttpGet]
    [ActionName("Get_UserID")]
    [IdentityBasicAuthentication]
    [Authorize]
    public HttpResponseMessage Get_UserID(string userName)
    {
        HttpResponseMessage res = new HttpResponseMessage(HttpStatusCode.Created);
        try
        {
            var user = Membership.GetUser(userName, false);
            if (user != null)
            {
                res = Request.CreateResponse(HttpStatusCode.OK, (Guid)user.ProviderUserKey);
            }
            else
            {
                res = Request.CreateResponse(HttpStatusCode.ExpectationFailed);
                res.Content = new StringContent("Error");
                res.ReasonPhrase = "UserName not find in the database";
            }
        }
        catch (Exception exc)
        {
            //Set the response message as an exception
            res = Request.CreateResponse(HttpStatusCode.InternalServerError);
            res.Content = new StringContent("Exception");
            res.ReasonPhrase = exc.Message;
        }
        return res;
    }

**

客户端 - 我如何调用 web api 并构建我的 httpClient

**

    public static async Task<HttpResponseMessage> RequestStart(string requestUrl, string webApiUrlBase = Globals.WebApi_Url, bool IsAuthenticateMemberRequest = false)
    {
        if (webApiUrlBase == null)
        {
            webApiUrlBase = Globals.WebApi_Url;
        }
        var response = new HttpResponseMessage(HttpStatusCode.Created);

        using (var client = new HttpClient())
        {
            if (IsAuthenticateMemberRequest)
            {
                string strToEncode = ApplicationData.Current.LocalSettings.Values["userName"].ToString() + ":" + ApplicationData.Current.LocalSettings.Values["password"].ToString();
                var authenticationBytes = Encoding.ASCII.GetBytes(strToEncode);

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(authenticationBytes));
            }
            client.BaseAddress = new Uri(Globals.WebApi_Url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            response = await client.GetAsync(requestUrl);
        }

        return response;
    }

**

IIS 配置(appPool => NetworkServices - 集成)

**

**

提琴手调试

**

【问题讨论】:

  • 能贴出代码吗?
  • 请不要将您的代码显示为图片。请编辑您的问题,将其替换为从 IDE 剪切和粘贴的代码。完成后,选择它并按 Ctrl-K 将其格式化为代码块。
  • 对不起,我可以,我现在才更新。但正如我所见,该代码在(本地)模式下工作得很好。也许问题出在我的 IIS 服务器上

标签: c# asp.net-web-api asp.net-web-api2 basic-authentication


【解决方案1】:

终于自己找了好几遍,好几个小时。我找到了解决方案。我们永远不应该启用基本身份验证...... 我知道这很奇怪 ^^ 但是如果您想使用自定义基本身份验证。只需在 IIS 上禁用基本身份验证,一切顺利。

【讨论】:

  • 你真是个天才,我花了 2 天时间来修复它。遵循有关文件夹权限和类似内容的其他 SO 答案,但这是唯一对我有用的东西。谢谢!!!!
  • 我很高兴知道,我帮助别人回答我的问题^^@Ibrahim D.
  • 我有同样的问题,但我尝试更改为禁用基本身份验证,它不起作用:(
  • 是的!谢谢!我被这个问题困了几个小时。但这现在完全有道理。通过在 IIS 中打开 Basic Auth,我告诉 IIS 它应该使用 Basic Auth 标头对请求进行身份验证。但是如果 IIS 不能验证用户,它会拒绝他们。由于我的应用程序提供身份验证逻辑(带有数据库查找),因此 IIS 不应该也不能,因此需要在 IIS 中禁用它。见相关:stackoverflow.com/a/5373530/579148
猜你喜欢
  • 2018-07-09
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 2014-08-24
  • 2018-12-15
  • 2021-04-24
  • 2012-10-02
  • 2013-08-09
相关资源
最近更新 更多