2016-12-01 69 views
2

我遇到了CORS通过Servicestack C#API的问题。我有一个通过在Microsoft服务器上运行的nodejs后端服务的angularjs应用程序。 NodeJS提供的角度项目很好,节点本身没有联系我运行在不同域但在同一台服务器上的ServiceStack服务的问题。当我需要进行Microsft Active Directory调用以获取当前活动用户时,会出现问题。为了正确地进行这个调用,我必须从我的angularjs项目中调用ServiceStack服务。当我拨打这个电话时,我收到No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://domain1:8080' is therefore not allowed access我有其他项目,我可以用同样的电话拨打,但由于某种原因,我似乎无法完成此项工作。CORS问题与C#Servicestack和NodeJS

Angularjs调用(http://domain1:8080):

$http.get("http://domain2/dochelper/GetActiveAccount?format=json", { headers: { "siteprefix": prefix } }) 
     .then(function successCallback(resp) { 
      console.log(resp.data); 
     }, function errorCallback(resp) { 

     }); 

Servicestack要求:

[Route("/GetActiveAccount")] 
public class GetActiveAccount 
{ 
    public string Id { get; set; } 
} 

[Authenticate] 
public AccountDTO Get(GetActiveAccount request) 
{ 
    AccountDTO obj = new AccountDTO(); 

    var ses = this.GetSession() as AuthUserSession; 

    return obj; 
} 

Servicestack CORS配置:

  Plugins.Add(new CorsFeature(allowOriginWhitelist: new[] { "http://winexpresstest:8080" }, 
       allowedMethods: "GET, POST, PUT, DELETE, OPTIONS", 
       allowedHeaders: "Content-Type, Authorization, Session-Id, ViewPort-Width, ViewPort-PixelRatio, Accept-Ranges, Pragma, Cache-Control, If-Modified-Since, Access-Control-Allow-Origin, siteprefix", 
       allowCredentials: true)); 

      Plugins.Add(new AuthFeature(() => new AuthUserSession(), new ServiceStack.Auth.IAuthProvider[] { 
       new AspNetWindowsAuthProvider(this) { 
        LoadUserAuthFilter = LoadUserAuthInfo, 
        AllowAllWindowsAuthUsers = true 
       }, 
      })); 

     } 

回答

3

你要指定白名单的起源让域名在Access-Control-Allow-Origin HTTP响应hea中明确列出例如:

Plugins.Add(new CorsFeature(allowOriginWhitelist: new [] { "http://domain1:8080" }, 
    allowedMethods: "GET, POST, PUT, DELETE, OPTIONS", 
    allowCredentials: false)); 
+0

'allowedOrigins:“http:/ domain1:8080”'将此添加到CorsFeature中似乎没有任何改变。 – Ohjay44

+0

@ Ohjay44您需要使用接受'allowOriginWhitelist'的构造函数,并在我的示例中看到一组域。 – mythz

+0

那是哪个构造函数?我目前使用ServiceStack构造函数。 – Ohjay44