我创建了一个自托管 Owin/SignalR 应用程序,其代码类似于本教程中的代码:
I've created a self hosting Owin/SignalR app with code similar to the code in this tutorial:
SignalR 自托管教程
一切正常,但为了安全起见,我想将其限制为仅允许来自特定远程站点的消息.换句话说,我想替换app.UseCors(CorsOptions.AllowAll);"符合代码以将应用程序限制为仅响应来自我定义的 URL 的消息,即只允许来自例如 http://www.remote_site 的消息.com 什么的.有什么简单的方法可以做到这一点?
Everything works, but for security-sake, I'd like to limit it to only allow messages from a specific remote site. In other words, I'd like to replace the "app.UseCors(CorsOptions.AllowAll);" line with code to confine the app to only responding to messages from a URL that I define, i.e. only allow messages from, say, http://www.remote_site.com or something. Is there any easy way to do this?
作为参考,这里是我的 SignalR 启动类的代码:
For reference, here is the code for my SignalR startup 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
类的完整实现:
Here is the full implementation of the 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
.
Also, if you want to server to accept a list of domains, you simply add them to the Origins
.
希望这会有所帮助!祝你好运!
Hope this helps! Good luck!
这篇关于在 Owin 启动类中指定域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!