【发布时间】:2016-01-20 10:35:09
【问题描述】:
我发誓这种事在我身上发生过很多次,以至于我真的讨厌 CORS。 我刚刚将我的应用程序一分为二,一个只处理 API 端的东西,另一个处理客户端的东西。 我以前做过这个,所以我知道我需要确保 CORS 已启用并允许所有,所以我在 WebApiConfig.cs
中进行了设置public static void Register(HttpConfiguration config)
{
// Enable CORS
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
// Web API configuration and services
var formatters = config.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var serializerSettings = jsonFormatter.SerializerSettings;
// Remove XML formatting
formatters.Remove(config.Formatters.XmlFormatter);
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
// Configure our JSON output
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
serializerSettings.Formatting = Formatting.Indented;
serializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
serializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
// Configure the API route
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
如您所见,我的第一行启用了 CORS,所以它应该可以工作。 如果我打开我的客户端应用程序并查询 API,它确实可以工作(没有 EnableCors,我会收到预期的 CORS 错误。 问题是我的 /token 仍然出现 CORS 错误。现在我知道 /token 端点不是 WebAPI 的一部分,所以我创建了自己的 OAuthProvider(我必须指出它在其他地方也可以使用),看起来像这样:
public class OAuthProvider<TUser> : OAuthAuthorizationServerProvider
where TUser : class, IUser
{
private readonly string publicClientId;
private readonly UserService<TUser> userService;
public OAuthProvider(string publicClientId, UserService<TUser> userService)
{
if (publicClientId == null)
throw new ArgumentNullException("publicClientId");
if (userService == null)
throw new ArgumentNullException("userService");
this.publicClientId = publicClientId;
this.userService = userService;
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
var user = await this.userService.FindByUserNameAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var oAuthIdentity = this.userService.CreateIdentity(user, context.Options.AuthenticationType);
var cookiesIdentity = this.userService.CreateIdentity(user, CookieAuthenticationDefaults.AuthenticationType);
var properties = CreateProperties(user.UserName);
var ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
context.AdditionalResponseParameters.Add(property.Key, property.Value);
return Task.FromResult<object>(null);
}
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
// Resource owner password credentials does not provide a client ID.
if (context.ClientId == null)
{
context.Validated();
}
return Task.FromResult<object>(null);
}
public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context)
{
if (context.ClientId == this.publicClientId)
{
var redirectUri = new Uri(context.RedirectUri);
var expectedRootUri = new Uri(context.Request.Uri, redirectUri.PathAndQuery);
if (expectedRootUri.AbsoluteUri == redirectUri.AbsoluteUri)
context.Validated();
}
return Task.FromResult<object>(null);
}
public static AuthenticationProperties CreateProperties(string userName)
{
IDictionary<string, string> data = new Dictionary<string, string>
{
{ "userName", userName }
};
return new AuthenticationProperties(data);
}
}
如您所见,在 GrantResourceOwnerCredentials 方法中,我再次启用了对所有内容的 CORS 访问。这应该适用于对 /token 的所有请求,但它不是。 当我尝试从客户端应用程序登录时,出现 CORS 错误。 Chrome 显示:
XMLHttpRequest 无法加载 http://localhost:62605/token。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://localhost:50098' 不允许访问。响应的 HTTP 状态代码为 400。
Firefox 显示:
跨域请求被阻止:同源策略不允许读取位于http://localhost:62605/token 的远程资源。 (原因:缺少 CORS 标头“Access-Control-Allow-Origin”)。 跨域请求被阻止:同源策略不允许读取位于http://localhost:62605/token 的远程资源。 (原因:CORS 请求失败)。
出于测试目的,我决定使用 fiddler 来查看是否可以看到其他任何可能给我关于正在发生的事情提供线索的东西。当我尝试登录时,FIddler 显示响应代码为 400,如果我查看原始响应,我可以看到错误:
{"error":"unsupported_grant_type"}
这很奇怪,因为我发送的数据没有改变,并且在拆分之前工作正常。 我决定在提琴手上使用 Composer 并复制我期望 POST 请求的样子。 当我执行它时,它工作正常,我得到一个 200 的响应代码。
有人知道为什么会发生这种情况吗?
更新 1
仅供参考,来自我的客户端应用程序的请求如下所示:
OPTIONS http://localhost:62605/token HTTP/1.1
Host: localhost:62605
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: POST
Origin: http://localhost:50098
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36
Access-Control-Request-Headers: accept, authorization, content-type
Accept: */*
Referer: http://localhost:50098/account/signin
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
来自作曲家,它看起来像这样:
POST http://localhost:62605/token HTTP/1.1
User-Agent: Fiddler
Content-Type: 'application/x-www-form-urlencoded'
Host: localhost:62605
Content-Length: 67
grant_type=password&userName=foo&password=bar
【问题讨论】:
标签: asp.net asp.net-web-api cors