Security exception when using tcpclient

K

Kristian

Hi All.

This test code:

using System.Net.Sockets;
using System.Net;
using System;

class mail{
public static void Main(){
Console.WriteLine("** INIT **");
TcpClient tcp = new TcpClient();
try{
tcp.Connect("127.0.0.1", 9001 );
}catch( Exception e ){
Console.Write( e );
}
}
}

Generates the Exception you see below.

System.Security.SecurityException: Anmodningen om tilladelsen af typen
System.Ne
t.SocketPermission, System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=
b77a5c561934e089 mislykkedes.
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32
port)
at mail.Main() in i:\CSharpDev\Mail\main.cs:line

Does any one know why? As far as I have been able to figure out, it
has something to do whith the
C:\WINNT\Microsoft.NET\Framework\v1.1.4322\mscorcfg settings, but no
matter what I try, I still get the same error.

Im running on win2000 and have administrators rigths to my machine.
 
E

Ed Courtenay

Kristian said:
Hi All.

This test code:

using System.Net.Sockets;
using System.Net;
using System;

class mail{
public static void Main(){
Console.WriteLine("** INIT **");
TcpClient tcp = new TcpClient();
try{
tcp.Connect("127.0.0.1", 9001 );
}catch( Exception e ){
Console.Write( e );
}
}
}

Generates the Exception you see below.

System.Security.SecurityException: Anmodningen om tilladelsen af typen
System.Ne
t.SocketPermission, System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=
b77a5c561934e089 mislykkedes.
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32
port)
at mail.Main() in i:\CSharpDev\Mail\main.cs:line

Does any one know why? As far as I have been able to figure out, it
has something to do whith the
C:\WINNT\Microsoft.NET\Framework\v1.1.4322\mscorcfg settings, but no
matter what I try, I still get the same error.

Im running on win2000 and have administrators rigths to my machine.


The context in which this code is running does not have the right to
open a socket; are you executing this from a network share or from an
internet resource?

Although it doesn't elevate the privileges, it makes sense to mark your
application with the appropriate code access security attributes - this
means that the application will not start at all unless the
appropriate permissions are available for the application.

In the case of your code:

[assembly:SocketPermission(SecurityAction.RequestMinimum, Access =
"Connect", Host = "127.0.0.1", Transport = "Tcp", Port = "9001")]

or

[assembly:SocketPermission(SecurityAction.RequestMinimum, Unrestricted =
true)]
 
E

Ed Courtenay

Chad said:
(e-mail address removed) (Kristian) wrote in



Can you translate that? Not many of us read Dannish?



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/

http://www.tranexp.com:2000/InterTran translates it as: "Request for
about the permission from the type"; it's the Danish version of the
"Request for the permission of type" error.

It's definately a code permissions issue
 
N

Nicholas Paldino [.NET/C# MVP]

Kristian,

Are you running your code in ASP.NET by chance? If so, then by default,
code in ASP.NET runs under the ASPNET local account, which does not have
rights to the network. You will have to change the user that the page runs
under, or you will have to impersonate the user.

Hope this helps.
 
K

Kristian

Ed Courtenay said:
Kristian said:
Hi All.

This test code:

using System.Net.Sockets;
using System.Net;
using System;

class mail{
public static void Main(){
Console.WriteLine("** INIT **");
TcpClient tcp = new TcpClient();
try{
tcp.Connect("127.0.0.1", 9001 );
}catch( Exception e ){ Console.Write( e );
}
}
}

Generates the Exception you see below.

System.Security.SecurityException: Anmodningen om tilladelsen af typen
System.Ne
t.SocketPermission, System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=
b77a5c561934e089 mislykkedes.
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32
port)
at mail.Main() in i:\CSharpDev\Mail\main.cs:line

Does any one know why? As far as I have been able to figure out, it
has something to do whith the
C:\WINNT\Microsoft.NET\Framework\v1.1.4322\mscorcfg settings, but no
matter what I try, I still get the same error.

Im running on win2000 and have administrators rigths to my machine.


The context in which this code is running does not have the right to
open a socket; are you executing this from a network share or from an
internet resource?

Although it doesn't elevate the privileges, it makes sense to mark your
application with the appropriate code access security attributes - this
means that the application will not start at all unless the
appropriate permissions are available for the application.

In the case of your code:

[assembly:SocketPermission(SecurityAction.RequestMinimum, Access =
"Connect", Host = "127.0.0.1", Transport = "Tcp", Port = "9001")]

or

[assembly:SocketPermission(SecurityAction.RequestMinimum, Unrestricted =
true)]

Hi Ed.
I tryed to move the .csc files from my dev drive and to my C: drive,
by doing this there was no exception thrown, so I guess your rigth, I
am running from a network share. Ill try to add the code you entered,
just to check what happens.

Thank you for your reply


/Kristian
 
W

Willy Denoyette [MVP]

Nicholas ,

Account privileges do not apply to (non raw)sockets.

Willy.

Nicholas Paldino said:
Kristian,

Are you running your code in ASP.NET by chance? If so, then by
default,
code in ASP.NET runs under the ASPNET local account, which does not have
rights to the network. You will have to change the user that the page
runs
under, or you will have to impersonate the user.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kristian said:
Hi All.

This test code:

using System.Net.Sockets;
using System.Net;
using System;

class mail{
public static void Main(){
Console.WriteLine("** INIT **");
TcpClient tcp = new TcpClient();
try{
tcp.Connect("127.0.0.1", 9001 );
}catch( Exception e ){
Console.Write( e );
}
}
}

Generates the Exception you see below.

System.Security.SecurityException: Anmodningen om tilladelsen af typen
System.Ne
t.SocketPermission, System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=
b77a5c561934e089 mislykkedes.
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32
port)
at mail.Main() in i:\CSharpDev\Mail\main.cs:line

Does any one know why? As far as I have been able to figure out, it
has something to do whith the
C:\WINNT\Microsoft.NET\Framework\v1.1.4322\mscorcfg settings, but no
matter what I try, I still get the same error.

Im running on win2000 and have administrators rigths to my machine.
 
Top