Specify Domain in Owin Startup Class(在 Owin 启动类中指定域)
问题描述
我创建了一个自托管 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 启动类中指定域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Owin 启动类中指定域
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01