使用 ConfigurationValidator 或其他验证器验证 Uri

发布于 2024-08-17 13:26:49 字数 387 浏览 0 评论 0原文

在配置文件的自定义配置部分中,我希望属性或元素为服务端点定义新方案、主机和端口,但不定义路径。 因此,应该允许 https://newhost/http://newhost:800,但不允许 newhost:800http://newhost/path/to/service

实施的最佳选择是什么?

感觉应该有一个很好的 Uri.Parse、UriParser 重载,这会让事情变得容易。我是否错过了 UriValidator?或者正则表达式将是最好的选择(这样它就可以轻松地禁止该路径)?

请注意,这并不是特定于 ConfigurationValidator,因为可以重用的通用验证器会很有用。

In a custom configuration section of a config file I want to have properties or elements define a new scheme, host and port for a service endpoint, but not define the path.
So these should be allowed https://newhost/ or http://newhost:800, but not newhost:800, http://newhost/path/to/service.

What would be the best option to implement?

It feels like there should be a good overload of a Uri.Parse, UriParser that would make it easy. Is there a UriValidator that I have missed? Or is regex going to be the best option (so that it can easily disallow the path)?

Note that this isn't specific to a ConfigurationValidator as a general validator that can be reused would be useful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

破晓 2024-08-24 13:26:49

Uri 类的 GetLeftPart 方法始终可以提供帮助。

GetLeftPart 方法

GetLeftPart 方法从UriPartial 系统枚举,其中之一 (UriPartial.Authority)将返回:

方案和权限部分
URI。

这有效地删除了原始字符串中可能存在的任何无关路径信息,并且如果提供的 Uri 不包含有效的 scheme (即 Uri 的 http 或 https 等部分)和/或权限(即在您的示例中,这是Uri 的 newhost 部分)。

从这里开始,您应该能够将 GetLLeftPart 调用的返回值与原始 Uri 字符串进行比较,如果它们不同,则该 Uri 为“无效”。如果它们相同,则 Uri 是“有效的”(对于您的目的而言)。

下面是一个简单的示例类,它将执行此“验证”,为 Uri 返回 True 或 False(C# 和 VB.NET 版本):

C#

public static class UriValidator
{
    public static bool IsUriValid(string uri)
    {
        try
        {
            string original_uri = uri;
            if (uri.Substring(uri.Length - 1, 1) == "/")
            {
                original_uri = uri.Substring(0, uri.Length - 1);
            }
            Uri myUri = new Uri(original_uri);
            string new_uri = myUri.GetLeftPart(UriPartial.Authority);
            if (original_uri.ToLower() == new_uri.ToLower())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }            
    }
}

VB.NET

Public Class UriValidator
    Public Shared Function IsUriValid(ByVal uri As String) As Boolean
        Try
            Dim original_uri = uri
            If uri.Substring(uri.Length - 1, 1) = "/" Then
                original_uri = uri.Substring(0, uri.Length - 1)
            End If
            Dim myUri As Uri = New Uri(original_uri)
            Dim new_uri As String = myUri.GetLeftPart(UriPartial.Authority)
            If original_uri.ToLower = new_uri.ToLower Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class

我运行使用此类进行一个简单的测试:

Console.WriteLine("https://newhost/" + "  " + UriValidator.IsUriValid("https://newhost/"));
Console.WriteLine("http://newhost:800" + "  " + UriValidator.IsUriValid("http://newhost:800"));
Console.WriteLine("newhost:800" + "  " + UriValidator.IsUriValid("newhost:800"));
Console.WriteLine("newhost:" + "  " + UriValidator.IsUriValid("newhost:"));
Console.WriteLine("qwerty:newhost" + "  " + UriValidator.IsUriValid("qwerty:newhost"));
Console.WriteLine("qwerty://newhost" + "  " + UriValidator.IsUriValid("qwerty://newhost"));
Console.WriteLine("qwerty://newhost:800" + "  " + UriValidator.IsUriValid("qwerty://newhost:800"));
Console.WriteLine("http://newhost/path/to/service" + "  " + UriValidator.IsUriValid("http://newhost/path/to/service"));

它给出了以下输出:

https://newhost/ 正确
http://newhost:800 正确
新主机:800 假
新主机:错误
qwerty:newhost False
qwerty://newhost True
qwerty://newhost:800 正确
http://newhost/path/to/service False

这似乎就是您所追求的!

请注意,像 qwerty://newhost 这样的 Uri 仍然验证为 True,因为 qwerty可能 是在您的系统上注册的有效协议。如果您只想允许 http 和/或 https,那么添加它应该很简单。

There is always the GetLeftPart method of the Uri class that could help here.

GetLeftPart Method

The GetLeftPart method takes an enumeration from the UriPartial system enumeration, one of which (UriPartial.Authority) will return:

The scheme and authority segments of
the URI.

This effectively removes any extraneous path information that may be in the original string, and will usually return a zero-length string if the Uri supplied does not contain a valid scheme (i.e. the http or https etc. parts of the Uri) and/or authority (i.e. in your example, this is the newhost part of the Uri).

From here, you should be able to compare the return value of the call to GetLLeftPart with the original Uri string, and if they're different, the Uri is "invalid". If they're the same, the Uri is "valid" (for your purposes).

Here's a simple example class that will perform this "validation" returning either True or False for the Uri (both C# and VB.NET versions):

C#

public static class UriValidator
{
    public static bool IsUriValid(string uri)
    {
        try
        {
            string original_uri = uri;
            if (uri.Substring(uri.Length - 1, 1) == "/")
            {
                original_uri = uri.Substring(0, uri.Length - 1);
            }
            Uri myUri = new Uri(original_uri);
            string new_uri = myUri.GetLeftPart(UriPartial.Authority);
            if (original_uri.ToLower() == new_uri.ToLower())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }            
    }
}

VB.NET

Public Class UriValidator
    Public Shared Function IsUriValid(ByVal uri As String) As Boolean
        Try
            Dim original_uri = uri
            If uri.Substring(uri.Length - 1, 1) = "/" Then
                original_uri = uri.Substring(0, uri.Length - 1)
            End If
            Dim myUri As Uri = New Uri(original_uri)
            Dim new_uri As String = myUri.GetLeftPart(UriPartial.Authority)
            If original_uri.ToLower = new_uri.ToLower Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class

I ran a simple test using this class:

Console.WriteLine("https://newhost/" + "  " + UriValidator.IsUriValid("https://newhost/"));
Console.WriteLine("http://newhost:800" + "  " + UriValidator.IsUriValid("http://newhost:800"));
Console.WriteLine("newhost:800" + "  " + UriValidator.IsUriValid("newhost:800"));
Console.WriteLine("newhost:" + "  " + UriValidator.IsUriValid("newhost:"));
Console.WriteLine("qwerty:newhost" + "  " + UriValidator.IsUriValid("qwerty:newhost"));
Console.WriteLine("qwerty://newhost" + "  " + UriValidator.IsUriValid("qwerty://newhost"));
Console.WriteLine("qwerty://newhost:800" + "  " + UriValidator.IsUriValid("qwerty://newhost:800"));
Console.WriteLine("http://newhost/path/to/service" + "  " + UriValidator.IsUriValid("http://newhost/path/to/service"));

And it gave the following output:

https://newhost/ True
http://newhost:800 True
newhost:800 False
newhost: False
qwerty:newhost False
qwerty://newhost True
qwerty://newhost:800 True
http://newhost/path/to/service False

which seems to be what you're after!

Note that Uri's like qwerty://newhost still validate as True as qwerty could be a valid protocol registered on your system. If you only wanted to allow http and/or https, it should be trivial to add this in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文