Ho letto diversi post parlando di alcuni problemi simili, ma non riesco ancora a farlo al lavoro.MVC 5 OAuth con CORS
Sto eseguendo ajax su "Account/ExternalLogin" che genera ChallengeResult e avvia il flusso per l'autenticazione con OWIN.
Questo è il mio Startup
classe:
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);
}
}
sto permettendo CORS supportano whith linea app.UserCors(CorsOptinos.AllowAll);
E so l'intestazione viene aggiunto alla risposta perché ho intercettare l'evento OnApplyRedirect
e quando cerco l'origine è impostata su 'localhost: 443001' e l'intestazione 'Access-Control-Allow-Origin' è impostata anche su questo valore.
Tuttavia quando la risposta viene inviata al cliente ho il seguente errore:
XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxx No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin
https://localhost:44301
is therefore not allowed access.
Quello che mi manca qui.
Potrei ottenere un lavoro in giro facendo tutto questo "manualmente" (richiedendo direttamente google dal client ...) ma voglio davvero usare il middleware OWIN.