在 Owin 启动中指定域 Class
Specify Domain in Owin Startup Class
我创建了一个自托管 Owin/SignalR 应用程序,其代码类似于本教程中的代码:
一切正常,但为了安全起见,我想将其限制为仅允许来自特定远程站点的消息。换句话说,我想用代码替换 "app.UseCors(CorsOptions.AllowAll);" 行,以将应用程序限制为仅响应来自我定义的 URL 的消息,即只允许来自 [=13] 的消息=] 之类的。有什么简单的方法可以做到这一点吗?
作为参考,这是我的 SignalR 启动代码 class:
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
namespace SignalRSelfHost
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
// How do I only allow a specific URL instead of the "CorsOptions.AllowAll" option?
}
}
}
下面是 Owin Startup
class 的完整实现:
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Cors;
using System.Web.Cors;
[assembly: OwinStartup(typeof(SignalRSelfHost.Startup))]
namespace SignalRSelfHost
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
var policy = new CorsPolicy()
{
AllowAnyHeader = true,
AllowAnyMethod = true,
SupportsCredentials = true
};
policy.Origins.Add("domain"); //be sure to include the port:
//example: "http://localhost:8081"
app.UseCors(new CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = context => Task.FromResult(policy)
}
});
app.MapSignalR();
}
}
}
此外,如果您希望服务器接受域列表,只需将它们添加到 Origins
。
希望对您有所帮助!祝你好运!
这是我在上面评论中提到的代码:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(_corsOptions.Value);
app.MapSignalR();
}
private static Lazy<CorsOptions> _corsOptions = new Lazy<CorsOptions>(() =>
{
return new CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = context =>
{
var policy = new CorsPolicy();
policy.Origins.Add("http://localhost:8081");
policy.AllowAnyMethod = true;
policy.AllowAnyHeader = true;
policy.SupportsCredentials = true;
return Task.FromResult(policy);
}
}
};
});
}
这行得通,但我认为 Matei 上面的回答更清晰、更简单。
我创建了一个自托管 Owin/SignalR 应用程序,其代码类似于本教程中的代码:
一切正常,但为了安全起见,我想将其限制为仅允许来自特定远程站点的消息。换句话说,我想用代码替换 "app.UseCors(CorsOptions.AllowAll);" 行,以将应用程序限制为仅响应来自我定义的 URL 的消息,即只允许来自 [=13] 的消息=] 之类的。有什么简单的方法可以做到这一点吗?
作为参考,这是我的 SignalR 启动代码 class:
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
namespace SignalRSelfHost
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
// How do I only allow a specific URL instead of the "CorsOptions.AllowAll" option?
}
}
}
下面是 Owin Startup
class 的完整实现:
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Cors;
using System.Web.Cors;
[assembly: OwinStartup(typeof(SignalRSelfHost.Startup))]
namespace SignalRSelfHost
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
var policy = new CorsPolicy()
{
AllowAnyHeader = true,
AllowAnyMethod = true,
SupportsCredentials = true
};
policy.Origins.Add("domain"); //be sure to include the port:
//example: "http://localhost:8081"
app.UseCors(new CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = context => Task.FromResult(policy)
}
});
app.MapSignalR();
}
}
}
此外,如果您希望服务器接受域列表,只需将它们添加到 Origins
。
希望对您有所帮助!祝你好运!
这是我在上面评论中提到的代码:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(_corsOptions.Value);
app.MapSignalR();
}
private static Lazy<CorsOptions> _corsOptions = new Lazy<CorsOptions>(() =>
{
return new CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = context =>
{
var policy = new CorsPolicy();
policy.Origins.Add("http://localhost:8081");
policy.AllowAnyMethod = true;
policy.AllowAnyHeader = true;
policy.SupportsCredentials = true;
return Task.FromResult(policy);
}
}
};
});
}
这行得通,但我认为 Matei 上面的回答更清晰、更简单。