gpt4 book ai didi

c# - C# Servicestack 和 NodeJS 的 CORS 问题

转载 作者:搜寻专家 更新时间:2023-11-01 00:39:12 25 4
gpt4 key购买 nike

我通过 Servicestack C# API 遇到 CORS 问题。我有一个 angularjs 应用程序,它通过在 Microsoft 服务器上运行的 nodejs 后端提供服务。 NodeJS 很好地服务于 Angular 项目, Node 本身与我在不同域但在同一台服务器上运行的 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我还有其他项目,我可以很好地进行同样的调用,但由于某种原因,我似乎无法使这个工作正常进行。

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) {

});

服务栈请求:

[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;
}

服务栈 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
},
}));

}

最佳答案

您需要在白名单中指定来源,以便在 Access-Control-Allow-Origin HTTP 响应 header 中明确列出域,例如:

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

关于c# - C# Servicestack 和 NodeJS 的 CORS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40919982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com