在运行时添加其他白名单域,而无需重新启动网站

时间:2014-05-27 18:45:03

标签: servicestack

我使用此方法将白名单网站添加到我的多租户SaaS网站的api服务中:

   var whiteListCollection = new List<string>() {"http://localhost:1195","http://blah.com", "http://foo.net" };

var corsFeature = new CorsFeature(
               allowOriginWhitelist: whiteListCollection,
               allowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
               allowedHeaders: "Content-Type, Authorization, Accept, Origin, X-Requested-With",
               allowCredentials: true);

            Plugins.Add(corsFeature);

当通过管理界面配置新客户时,我希望能够更新此白名单而无需重新启动网站(然后从客户的数据库记录中重新创建whiteListCollection)。

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:2)

ServiceStack v4.0.21中的CorsFeature现在允许您访问AllowOriginWhitelist属性,因此可以使用以下内容进行修改:

var corsFeature = HostContext.GetPlugin<CorsFeature>();
corsFeature.AllowOriginWhitelist.Add(newUrl);

答案 1 :(得分:1)

您可以在代码中的任何位置操作插件集合,如下所示:

public void Toto()
{
    var plugin = EndpointHost.Plugins.FirstOrDefault(p => p is CorsFeature) as CorsFeature;
    if (plugin != null)
    {
        EndpointHost.Plugins.Remove(plugin);
    }
    var whiteListCollection = new List<string>() { "http://localhost:1195", "http://blah.com", "http://foo.net" };

    var corsFeature = new CorsFeature(
    allowOriginWhitelist: whiteListCollection,
    allowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
    allowedHeaders: "Content-Type, Authorization, Accept, Origin, X-Requested-With",
    allowCredentials: true);
    EndpointHost.Plugins.Add(corsFeature);
}