SQL Server security exception

R

RSH

I created a C# class that connects to a SQL Server. I am creating a object
on initialization but I keep getting a security exception:

"Request for the permission of type
'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."

I have used this connection string before with no problems...what am I
overlooking here?

Thanks,
Ron


using System;

using System.Collections.Generic;

using System.Text;

using System.Data.SqlClient;

using System.Data;

namespace GeneralTesting

{

class DataClass

{

public SqlConnection SqlConn = new SqlConnection();

public void setConnections()

{

String ConnectionString = "Data Source=TestServer; Integrated Security=SSPI;
Initial Catalog=master";

SqlConn.ConnectionString = ConnectionString;

SqlConn.Open();

}

public DataSet getEmployees(string CompanyID)

{


string strSelectSql = "SELECT FirstName, LastName FROM [" + CompanyID +
"].[dbo].[Employee]";

SqlCommand selectSqlCommand = new SqlCommand(strSelectSql, SqlConn);

SqlDataAdapter sqlData = new SqlDataAdapter(selectSqlCommand);

DataSet dsSelectData = new DataSet();

sqlData.Fill(dsSelectData);

return dsSelectData;

}

}

}





Initialization:

DataClass DC = new DataClass();

DC.setConnections();

DataSet DS = DC.getEmployees("00010145");
 
W

Willy Denoyette [MVP]

|
| I created a C# class that connects to a SQL Server. I am creating a
object
| on initialization but I keep getting a security exception:
|
| "Request for the permission of type
| 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0,
| Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
|
| I have used this connection string before with no problems...what am I
| overlooking here?
|
| Thanks,
| Ron
|
|
| using System;
|
| using System.Collections.Generic;
|
| using System.Text;
|
| using System.Data.SqlClient;
|
| using System.Data;
|
| namespace GeneralTesting
|
| {
|
| class DataClass
|
| {
|
| public SqlConnection SqlConn = new SqlConnection();
|
| public void setConnections()
|
| {
|
| String ConnectionString = "Data Source=TestServer; Integrated
Security=SSPI;
| Initial Catalog=master";
|
| SqlConn.ConnectionString = ConnectionString;
|
| SqlConn.Open();
|
| }
|
| public DataSet getEmployees(string CompanyID)
|
| {
|
|
| string strSelectSql = "SELECT FirstName, LastName FROM [" + CompanyID +
| "].[dbo].[Employee]";
|
| SqlCommand selectSqlCommand = new SqlCommand(strSelectSql, SqlConn);
|
| SqlDataAdapter sqlData = new SqlDataAdapter(selectSqlCommand);
|
| DataSet dsSelectData = new DataSet();
|
| sqlData.Fill(dsSelectData);
|
| return dsSelectData;
|
| }
|
| }
|
| }
|
|
|
|
|
| Initialization:
|
| DataClass DC = new DataClass();
|
| DC.setConnections();
|
| DataSet DS = DC.getEmployees("00010145");

Do you happen to run this from a network drive? Then you have to adapt your
code access security settings (CAS) for the share your code resides on, by
default, applications loaded from a network resource cannot access SQL
server. Check MSDN for the caspol utility for more info.

Willy.
 
R

RSH

Nope. The application is running locally, but the SQL server is on the
network. This is very strange because I'm using the same Connection String
here that I am in another application that is in the folder right next to
this one.

I am perplexed.
 
R

RSH

System.Security.SecurityException was unhandled
Message="Request for the permission of type
'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
Source="mscorlib"
StackTrace:
at System.Security.CodeAccessSecurityEngine.Check(Object demand,
StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.PermissionSet.Demand()
at System.Data.Common.DbConnectionOptions.DemandPermission()
at System.Data.SqlClient.SqlConnection.PermissionDemand()
at
System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection
outerConnection)
at
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at GeneralTesting.DataClass.setConnections()
at GeneralTesting.Program.Main()
 
R

RSH

I stand corrected apparently it was on a share.

I am still in quite a pickle here. I have read something on Strong naming
my assemblies...I tried to follow along but I was not able to make sense of
it...I have no DLL files in my project, I was able to make my .snk file but
it gets foggy after that. Is there some sort of Strong naming for Dummies
that I can follow along with? BTW I am running 2.0 framework.

Thanks for any help!
Ron
 
W

Willy Denoyette [MVP]

You don't need to strong name your assemblies for this to work, you only
need to grant full trust to your share.
For insatnce to grant Full trust to all of your assemblies loaded from your
share you can go with following command:

CasPol.exe -m -ag 1.2 -url file://Server/MyShare/* FullTrust


It's also possible to be less generous and create a new code group for this
with less privileges than Full trust.

Willy.

|I stand corrected apparently it was on a share.
|
| I am still in quite a pickle here. I have read something on Strong naming
| my assemblies...I tried to follow along but I was not able to make sense
of
| it...I have no DLL files in my project, I was able to make my .snk file
but
| it gets foggy after that. Is there some sort of Strong naming for Dummies
| that I can follow along with? BTW I am running 2.0 framework.
|
| Thanks for any help!
| Ron
|
|
|
| | >
| > I created a C# class that connects to a SQL Server. I am creating a
| > object on initialization but I keep getting a security exception:
| >
| > "Request for the permission of type
| > 'System.Data.SqlClient.SqlClientPermission, System.Data,
Version=2.0.0.0,
| > Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
| >
| > I have used this connection string before with no problems...what am I
| > overlooking here?
| >
| > Thanks,
| > Ron
| >
| >
| > using System;
| >
| > using System.Collections.Generic;
| >
| > using System.Text;
| >
| > using System.Data.SqlClient;
| >
| > using System.Data;
| >
| > namespace GeneralTesting
| >
| > {
| >
| > class DataClass
| >
| > {
| >
| > public SqlConnection SqlConn = new SqlConnection();
| >
| > public void setConnections()
| >
| > {
| >
| > String ConnectionString = "Data Source=TestServer; Integrated
| > Security=SSPI; Initial Catalog=master";
| >
| > SqlConn.ConnectionString = ConnectionString;
| >
| > SqlConn.Open();
| >
| > }
| >
| > public DataSet getEmployees(string CompanyID)
| >
| > {
| >
| >
| > string strSelectSql = "SELECT FirstName, LastName FROM [" + CompanyID +
| > "].[dbo].[Employee]";
| >
| > SqlCommand selectSqlCommand = new SqlCommand(strSelectSql, SqlConn);
| >
| > SqlDataAdapter sqlData = new SqlDataAdapter(selectSqlCommand);
| >
| > DataSet dsSelectData = new DataSet();
| >
| > sqlData.Fill(dsSelectData);
| >
| > return dsSelectData;
| >
| > }
| >
| > }
| >
| > }
| >
| >
| >
| >
| >
| > Initialization:
| >
| > DataClass DC = new DataClass();
| >
| > DC.setConnections();
| >
| > DataSet DS = DC.getEmployees("00010145");
| >
| >
| >
| >
| >
| >
|
|
 
R

RSH

Thanks for this info.

Is this going to be a problem for deployment also? ...or is this just in a
development environment?

Thanks,
Ron



Willy Denoyette said:
You don't need to strong name your assemblies for this to work, you only
need to grant full trust to your share.
For insatnce to grant Full trust to all of your assemblies loaded from
your
share you can go with following command:

CasPol.exe -m -ag 1.2 -url file://Server/MyShare/* FullTrust


It's also possible to be less generous and create a new code group for
this
with less privileges than Full trust.

Willy.

|I stand corrected apparently it was on a share.
|
| I am still in quite a pickle here. I have read something on Strong
naming
| my assemblies...I tried to follow along but I was not able to make sense
of
| it...I have no DLL files in my project, I was able to make my .snk file
but
| it gets foggy after that. Is there some sort of Strong naming for
Dummies
| that I can follow along with? BTW I am running 2.0 framework.
|
| Thanks for any help!
| Ron
|
|
|
| | >
| > I created a C# class that connects to a SQL Server. I am creating a
| > object on initialization but I keep getting a security exception:
| >
| > "Request for the permission of type
| > 'System.Data.SqlClient.SqlClientPermission, System.Data,
Version=2.0.0.0,
| > Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."
| >
| > I have used this connection string before with no problems...what am I
| > overlooking here?
| >
| > Thanks,
| > Ron
| >
| >
| > using System;
| >
| > using System.Collections.Generic;
| >
| > using System.Text;
| >
| > using System.Data.SqlClient;
| >
| > using System.Data;
| >
| > namespace GeneralTesting
| >
| > {
| >
| > class DataClass
| >
| > {
| >
| > public SqlConnection SqlConn = new SqlConnection();
| >
| > public void setConnections()
| >
| > {
| >
| > String ConnectionString = "Data Source=TestServer; Integrated
| > Security=SSPI; Initial Catalog=master";
| >
| > SqlConn.ConnectionString = ConnectionString;
| >
| > SqlConn.Open();
| >
| > }
| >
| > public DataSet getEmployees(string CompanyID)
| >
| > {
| >
| >
| > string strSelectSql = "SELECT FirstName, LastName FROM [" + CompanyID
+
| > "].[dbo].[Employee]";
| >
| > SqlCommand selectSqlCommand = new SqlCommand(strSelectSql, SqlConn);
| >
| > SqlDataAdapter sqlData = new SqlDataAdapter(selectSqlCommand);
| >
| > DataSet dsSelectData = new DataSet();
| >
| > sqlData.Fill(dsSelectData);
| >
| > return dsSelectData;
| >
| > }
| >
| > }
| >
| > }
| >
| >
| >
| >
| >
| > Initialization:
| >
| > DataClass DC = new DataClass();
| >
| > DC.setConnections();
| >
| > DataSet DS = DC.getEmployees("00010145");
| >
| >
| >
| >
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top