2017-08-14 2 views
0

ich benutze standard webapi ApplicationOAuthProvider code unten zu login. und ich füge hinzu inwebapi 2.0 cross herkunft verhalten

<add name="Access-Control-Allow-Origin" value="*" /> 

in der web.config und Client ist in der Lage über www.testapi.com/token einzuloggen. alles funktioniert gut.

Aber wenn ich eine benutzerdefinierte Webapi-Funktion erstellen. es bittet mich immer noch um Zugriffssteuerung zu aktivieren. Also ich dies tun, indem diese Codezeile Zugabe in WebapiConfig.cs

EnableCorsAttribute cors = new EnableCorsAttribute("http://www.myweb.com:82", "*", "*"); 
     config.EnableCors(cors); 

dieses Mal ist es die schnelle Fehlermeldung, dass

'' Access-Control-Allow-Origin-Header mehrere Werte enthält 'http://www.myweb.com:82, *' , aber nur einer ist erlaubt. Origin 'http://www.myweb.com:82' ist daher nicht erlaubt.

also entferne ich die <add name="Access-Control-Allow-Origin" value="*" /> in der web.config und es funktioniert !!.

Ich kehre zum Login zurück und es wird nach <add name="Access-Control-Allow-Origin" value="*" /> gefragt. aber wenn ich das hinzufügen, kann meine Webapi-Methode nicht aufrufen.

wenn ich nicht hinzufügen. Client ist nicht in der Lage sich einzuloggen.

Gibt es eine Möglichkeit für beide zu arbeiten? unten ist die Antwort von 200 mit Fehler. enter image description here

Update 1 startup.auth.cs

public void ConfigureAuth(IAppBuilder app) 
    { 
     // Configure the db context and user manager to use a single instance per request 
     app.CreatePerOwinContext(ApplicationDbContext.Create); 
     app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 

     // Enable the application to use a cookie to store information for the signed in user 
     // and to use a cookie to temporarily store information about a user logging in with a third party login provider 
     app.UseCookieAuthentication(new CookieAuthenticationOptions()); 
     app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

     // Configure the application for OAuth based flow 
     PublicClientId = "self"; 
     OAuthOptions = new OAuthAuthorizationServerOptions 
     { 
      TokenEndpointPath = new PathString("/Token"), 
      Provider = new ApplicationOAuthProvider(PublicClientId), 
      AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), 
      AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), 
      // In production mode set AllowInsecureHttp = false 
      AllowInsecureHttp = true 
     }; 

     // Enable the application to use bearer tokens to authenticate users 
     app.UseOAuthBearerTokens(OAuthOptions); 
     app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);//as instructed 

webapiconfig.cs

public static void Register(HttpConfiguration config) 
    { 


     // Web API configuration and services 
     // Configure Web API to use only bearer token authentication. 
     config.SuppressDefaultHostAuthentication(); 
     config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); 

     // Web API routes 
     config.MapHttpAttributeRoutes(); 

     config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 
     WebApiConfig.Register(config); 
     config.EnableCors(new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS, PUT, DELETE")); 
     //var jsonp = new JsonpMediaTypeFormatter(config.Formatters.JsonFormatter); 
     //config.Formatters.Insert(0, jsonp); 
    } 
} 
+0

Startup.Auth.cs

public void ConfigureAuth(IAppBuilder app) { app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);//working line 
nur hinzufügen Fügen Sie diese in Ihrem Api-Controller '[EnableCors (Herkunft: "*", headers: "*", Methoden: "*", exposedHeaders: "X-My-Header")] Dies ist die Controller-Ebene. Sie können dasselbe in einer bestimmten Aktionsmethode hinzufügen, um Kerne zu aktivieren. –

+0

es zeigt immer noch den gleichen Fehler "nur einer ist erlaubt. Ich habe den Zugriff-Ursprung in der web.config nicht entfernt. –

Antwort

3
  1. Microsoft.AspNet.WebApi.Cors Installieren
  2. 01 NuGet zu verpacken
  3. Microsoft.Owin.Cors installieren
  4. hinzufügen bei Startup.cs Datei config.EnableCors(new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS, PUT, DELETE")); die oben von WebApiConfig.Register(config); Linie nuget zu verpacken.
  5. Fügen Sie app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); in die Datei Startup.Auth.cs hinzu. Dies muss vor getan werden, um Aufruf IAppBuilder.UseWebApi
+0

immer noch der gleiche Fehler. Ich versuchte auch mit Versuch und Irrtum durch Entfernen der Zugriffsrichtlinie bei web.config immer noch das gleiche ... haizz ... das nervt mich wie verrückt –

1

ok schließlich gelang es mir, um es mit Hilfe zu bekommen arbeiten von „@manprit Singh Sahota“

i entfernen Sie alle Zugriffsrichtlinie von web.config. und auch unterhalb der Linie in WebApiConfig

EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*"); 
     config.EnableCors(cors); 

ich diese Linie zum enter image description here

Verwandte Themen