2016-06-03 4 views
0

Ich brauche OWIN Genehmigung von Web-api-Server zu implementieren. Unten ist meine Startup-Klasse.CORS Fehler auf anfordernden OWIN Token

[assembly: OwinStartup(typeof(SpaServerSide.MyStartUp))] 

namespace SpaServerSide 
{ 
    public class MyStartUp 
    { 
     public void Configuration(IAppBuilder app) 
     {    
      HttpConfiguration config = new HttpConfiguration(); 

      app.Map("/signalr", map => 
      { 
       map.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 
       var hubConfig = new Microsoft.AspNet.SignalR.HubConfiguration { }; 
       map.RunSignalR(hubConfig); 
      }); 


      app.UseCookieAuthentication(new CookieAuthenticationOptions() 
      { 
       AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
       LoginPath = new PathString("/#") 
      }); 


      OAuthAuthorizationServerOptions OAuthOptions = new OAuthAuthorizationServerOptions() 
      { 
       AllowInsecureHttp = true, 
       TokenEndpointPath = new PathString("/Token"), 
       AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(5), 
       Provider = new SpaServerSide.Shared.OAuthTokenServer()    
      }; 

      app.UseOAuthAuthorizationServer(OAuthOptions); 
      app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); 

      WebApiConfig.Register(config); 
      app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 
      app.UseWebApi(config); 

     } 
    } 
} 

Dann implementieren ich die OAuthAuthorizationServerProvider wie die folgenden:

public class OAuthTokenServer : OAuthAuthorizationServerProvider 
    { 
     public ASPIdentityUserManager cusUserManager;  

     public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
     { 
      context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); 
      context.OwinContext.Response.Headers.Add("Access-Control-Allow-Credentials", new[] { "true" }); 
      var user = await cusUserManager.FindAsync(context.UserName, context.Password); 
      if (user == null) 
      { 
       context.SetError("invalid_grant", "Username and password do not match."); 
       return; 
      } 
      var identity = await cusUserManager.CreateIdentityAsync(user, context.Options.AuthenticationType); 
      context.Validated(identity); 
     } 

    } 

Danach habe ich den Web-Server auf http://localhost:5587 und Client-Web-Site auf http://localhost gehostet haben. Als ich versuchte, das Token mit Angular JS anzufordern, warf der Browser einen CORS-Fehler auf. Die Meldung lautet wie folgt:

Cross-Origin-Anforderung blockiert: Die Same Origin Policy nicht zulässt die Remote-Ressource bei http://localhost:5587/Token lesen. (Grund: CORS Header 'Access-Control-Allow-Origin' fehlt).

Bitte schlagen Sie mir alles vor, was ich verpasst hätte.

Antwort

1

die Zeile verschieben: app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

zu Beginn Ihrer Configuration() Methode.

Sie haben CORS Middleware vor oauth Middleware zu konfigurieren. Und vor Signal Middleware, wenn Sie es brauchen.

0
+0

Ein Zitat von einem der Link > Tun Sie nichts an den Browser. CORS wird standardmäßig in allen modernen Browsern (und seit Firefox 3.5) unterstützt. Der Server von JavaScript zugegriffen wird, hat die Website-Hosting das HTML-Dokument zu geben, in dem die JS Erlaubnis HTTP-Response-Header über CORS läuft. –

0

Ich denke, u aktivieren müssen CORS in Ihre Serverseite. U kann sich darauf beziehen http://enable-cors.org/server.html. Klicken Sie auf den Link auf Ihrem Server.

Hoffnung, dass u helfen. :)

+0

versucht, dass 'app.UseCors (Microsoft.Owin.Cors.CorsOptions.AllowAll);' auf der StartUp-Klasse und 'context.OwinContext.Response.Headers.Add ("Access-Control-Allow-Origin", new [] { "*"}); 'in der Provider-Klasse. –