wcf - 从 WCF 服务调用 Web API Core 时出现 401 错误
问题描述
我正在尝试从 WCF 服务调用 .net 核心 Web api。
这是我的代码:
var client = new HttpClient();
client.BaseAddress = new Uri("http://...");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("api/tasks");
return await response.Content.ReadAsStringAsync();
响应返回 401 错误:未经授权。
我的 web api 支持 windows 和匿名身份验证,我在允许的 cors 来源中添加了“*”:
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.WithOrigins("http://localhost:4200","http://...", "*");
}));
我的 web api 控制器中的 get 函数是:
// GET api/tasks
[AllowAnonymous]
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
仍然是相同的错误响应。知道为什么吗?
解决方案
我认为您的网址中有错误。
尝试这个:
HttpResponseMessage response = await client.GetAsync("api/tasks/get");
推荐阅读
- java - 如何计算数组中字符串值中的字符数,然后将其与其他字符进行比较以找到最长的字符?
- javascript - 对于一个用户交互高的网站,客户端渲染好还是服务器端渲染好?
- python - Python,如果键已经存在,如何将键+数字添加到字典
- database - 搜索数据库的算法
- python - 警报 Python Selenium、Chrome WebDriver:切换到警报不起作用
- python - 种群递归
- azure-devops - 在 Azure Pipeline 中找不到已发布的工件
- python - 在另一个函数中使用创建的类时出现 Python 错误
- sql - 使用多个计数/总和跨多个字段进行透视
- php - 如果在 MySQL 中满足条件,则同时执行 SELECT 和 UPDATE