从网络共享运行时,具有udp协议的C#exe会引发Socket权限异常. [英] C# exe with udp protocol, when runs from network share throws Socket permission exception.

查看:102
本文介绍了从网络共享运行时,具有udp协议的C#exe会引发Socket权限异常.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口应用程序,当它从本地PC运行时,它执行时没有错误.
但是,当我从网络运行相同的exe时,例如\\ Server \ Share \ ExeName.exe,它会引发以下异常.

I have a window application which, when it runs from local pc, it executes with no error.
However, when I run the same exe from network e.g. \\Server\Share\ExeName.exe, it gives throws an exception as below.

<br />
Application attempted to perform an operation not allowed by the security policy. <br />
To grant this application the required permission, contact your....<br />
Request for permission of type ''System.Net.SockedtPermission, System, Version=2.0.0.0,<br />
Culture=neutral, PublicKeyToken=b77...'' failed<br />


我的代码:


My code:

private void BindPort()
{
Socket winsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostEntry.AddressList[0];         
SocketPermission permitSocket = new SocketPermission(NetworkAccess.Connect, TransportType.Udp, ipAddress.ToString(), 1007);
permitSocket.Assert();
EP = new IPEndPoint(ipAddress, 1007);
try
{
     winsock.Bind(EP);         
}
catch (SocketException exp)
{
     MessageBox.Show(exp.Message);  
     return false;
}
return true;
}

推荐答案


从本地计算机运行应用程序时,它已设置了完全信任"权限,因此在使用套接字时没有问题.但是,如果您通过网络运行应用程序,则它是本地Intranet区域的一部分,该区域具有本地Intranet权限设置.为了更改本地Intranet区域的权限集,您应该访问.NET配置工具(mscorcfg.msc),并手动进行更改.在codeproject上有一篇很棒的文章,它详细解释了.NET环境中的代码访问安全性-了解.NET代码访问安全性 [^ ]
问候
Hi,
When you run your application from your local computer it has the Full Trust permission set, thus you have no problem in working with sockets. But if you run you app from network it is a part of Local Intranet Zone which has the Local Intranet Permission set. In order to change the permission set for Local Intranet Zone, you should access the .NET Configuration tool (mscorcfg.msc), and change it manually. There is a great article here on codeproject that explain in a great detail Code Access Security within the .NET environment - Understanding .NET Code Access Security[^]
Regards


这篇关于从网络共享运行时,具有udp协议的C#exe会引发Socket权限异常.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆