在 Owin 启动类中指定域

编程入门 行业动态 更新时间:2024-10-24 11:20:59
本文介绍了在 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 的消息 什么的.有什么简单的方法可以做到这一点?

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 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 启动类中指定域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-19 14:56:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/652026.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   Owin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!