【发布时间】:2015-02-06 12:19:18
【问题描述】:
我阅读了几篇谈论一些类似问题的帖子,但我还没有这样做。
我正在对“Account/ExternalLogin”执行 ajax,它会生成 ChallengeResult 并开始使用 OWIN 进行身份验证。
这是我的Startup 课程:
public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
// Use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseCors(CorsOptions.AllowAll);
var goath2 = new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationOptions
{
ClientId = "myclientid",
ClientSecret = "mysecret",
Provider = new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationProvider
{
OnApplyRedirect = context =>
{
string redirect = context.RedirectUri;
const string Origin = "Origin";
const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
// origin is https://localhost:44301
var origin = context.Request.Headers.GetValues(Origin).First();
// header is present
var headerIsPresent = context.Response.Headers.ContainsKey(AccessControlAllowOrigin);
context.Response.Redirect(redirect);
}
}
};
app.UseGoogleAuthentication(goath2);
}
}
我正在通过 app.UserCors(CorsOptinos.AllowAll); 行启用 CORS 支持
而且我知道标头已添加到响应中,因为我拦截了OnApplyRedirectevent,当我查找来源时,它被设置为“localhost:443001”并且标头“Access-Control-Allow-Origin”也被设置到这个值。
但是,当响应发送到客户端时,我遇到以下错误:
XMLHttpRequest 无法加载 https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxx 请求中不存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点
https://localhost:44301访问。
我在这里缺少什么。
我可以“手动”解决所有这些问题(直接从客户端请求谷歌...),但我真的想使用 OWIN 中间件。
【问题讨论】:
标签: ajax asp.net-mvc oauth-2.0 cors google-oauth