从网络共享运行时,具有udp协议的C#exe会引发Socket权限异常.

编程入门 行业动态 更新时间:2024-10-11 13:24:43
本文介绍了从网络共享运行时,具有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权限异常.

本文发布于:2023-11-13 06:01:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   权限   协议   网络   exe

发布评论

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

>www.elefans.com

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