ASP.NET MVC 4-401访问被拒绝 [英] ASP.NET MVC 4 - 401 Access Denied

查看:149
本文介绍了ASP.NET MVC 4-401访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个只返回一个整数的get命令创建了一个非常简单的WebAPI.当我在浏览器中运行它时,它可以很好地工作.但是,当我尝试从解决方案中的另一个项目访问问题时,我得到以下信息:

I've created a very simple WebAPI with one get command that just returns an integer. It works perfectly fine when I run it in a browser. But when I try to access problematically from another project in the solution I get the following:

2013-05-15 17:46:47,811-响应:状态代码:401,ReasonPhrase:'访问被拒绝',版本:1.1,内容:System.Net.Http.StreamContent,标头:

2013-05-15 17:46:47,811 - Response: StatusCode: 401, ReasonPhrase: 'Access Denied',Version: 1.1, Content: System.Net.Http.StreamContent, Headers:

{

日期:2013年5月15日,星期三,格林尼治标准时间

Date: Wed, 15 May 2013 16:46:47 GMT

服务器:Microsoft-IIS/5.1

Server: Microsoft-IIS/5.1

WWW身份验证:协商

WWW-Authenticate: Negotiate

WWW认证:NTLM

WWW-Authenticate: NTLM

连接:关闭

内容长度:4431

内容类型:text/html

Content-Type: text/html

}

这是我正在使用的代码.任何帮助将不胜感激.

Here is the code I'm using. Any help would really be appreciated.

var client = new HttpClient { BaseAddress = new Uri(_uri) };

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var response = client.GetAsync(string.Format("api/Config/{0}", id)).Result;

if (response.IsSuccessStatusCode)
{
    var res = response.Content.ReadAsAsync<int>().Result;
    return res;
}

推荐答案

由于响应表明您无权访问资源,可能是因为您没有传递适当的凭据.看来您的服务已通过Windows身份验证启用.尝试执行以下操作,看看是否可行:

As the response indicates you are not authorized to access the resource probably because you are not passing in proper credentials. Looks like your service is enabled with Windows Authentication. Try doing the following and see if it works:

HttpClientHandler hndlr = new HttpClientHandler();
hndlr.UseDefaultCredentials = true;
var client = new HttpClient(hndlr) { BaseAddress = new Uri(_uri) };

这篇关于ASP.NET MVC 4-401访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆