SQL Server security exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RSH

    SQL Server security exception


    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.Sq lClient.SqlClie ntPermission, System.Data, Version=2.0.0.0 ,
    Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed."

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

    Thanks,
    Ron


    using System;

    using System.Collecti ons.Generic;

    using System.Text;

    using System.Data.Sql Client;

    using System.Data;

    namespace GeneralTesting

    {

    class DataClass

    {

    public SqlConnection SqlConn = new SqlConnection() ;

    public void setConnections( )

    {

    String ConnectionStrin g = "Data Source=TestServ er; Integrated Security=SSPI;
    Initial Catalog=master" ;

    SqlConn.Connect ionString = ConnectionStrin g;

    SqlConn.Open();

    }

    public DataSet getEmployees(st ring CompanyID)

    {


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

    SqlCommand selectSqlComman d = new SqlCommand(strS electSql, SqlConn);

    SqlDataAdapter sqlData = new SqlDataAdapter( selectSqlComman d);

    DataSet dsSelectData = new DataSet();

    sqlData.Fill(ds SelectData);

    return dsSelectData;

    }

    }

    }





    Initialization:

    DataClass DC = new DataClass();

    DC.setConnectio ns();

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






  • Willy Denoyette [MVP]

    #2
    Re: SQL Server security exception


    "RSH" <way_beyond_oop s@yahoo.com> wrote in message
    news:OWhfyBWFGH A.3036@tk2msftn gp13.phx.gbl...
    |
    | 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.Sq lClient.SqlClie ntPermission, System.Data, Version=2.0.0.0 ,
    | Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed."
    |
    | I have used this connection string before with no problems...what am I
    | overlooking here?
    |
    | Thanks,
    | Ron
    |
    |
    | using System;
    |
    | using System.Collecti ons.Generic;
    |
    | using System.Text;
    |
    | using System.Data.Sql Client;
    |
    | using System.Data;
    |
    | namespace GeneralTesting
    |
    | {
    |
    | class DataClass
    |
    | {
    |
    | public SqlConnection SqlConn = new SqlConnection() ;
    |
    | public void setConnections( )
    |
    | {
    |
    | String ConnectionStrin g = "Data Source=TestServ er; Integrated
    Security=SSPI;
    | Initial Catalog=master" ;
    |
    | SqlConn.Connect ionString = ConnectionStrin g;
    |
    | SqlConn.Open();
    |
    | }
    |
    | public DataSet getEmployees(st ring CompanyID)
    |
    | {
    |
    |
    | string strSelectSql = "SELECT FirstName, LastName FROM [" + CompanyID +
    | "].[dbo].[Employee]";
    |
    | SqlCommand selectSqlComman d = new SqlCommand(strS electSql, SqlConn);
    |
    | SqlDataAdapter sqlData = new SqlDataAdapter( selectSqlComman d);
    |
    | DataSet dsSelectData = new DataSet();
    |
    | sqlData.Fill(ds SelectData);
    |
    | return dsSelectData;
    |
    | }
    |
    | }
    |
    | }
    |
    |
    |
    |
    |
    | Initialization:
    |
    | DataClass DC = new DataClass();
    |
    | DC.setConnectio ns();
    |
    | 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.



    Comment

    • RSH

      #3
      Re: SQL Server security exception


      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.


      "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
      news:OinWNHWFGH A.3984@TK2MSFTN GP14.phx.gbl...[color=blue]
      >
      > "RSH" <way_beyond_oop s@yahoo.com> wrote in message
      > news:OWhfyBWFGH A.3036@tk2msftn gp13.phx.gbl...
      > |
      > | 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.Sq lClient.SqlClie ntPermission, System.Data,
      > Version=2.0.0.0 ,
      > | Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed."
      > |
      > | I have used this connection string before with no problems...what am I
      > | overlooking here?
      > |
      > | Thanks,
      > | Ron
      > |
      > |
      > | using System;
      > |
      > | using System.Collecti ons.Generic;
      > |
      > | using System.Text;
      > |
      > | using System.Data.Sql Client;
      > |
      > | using System.Data;
      > |
      > | namespace GeneralTesting
      > |
      > | {
      > |
      > | class DataClass
      > |
      > | {
      > |
      > | public SqlConnection SqlConn = new SqlConnection() ;
      > |
      > | public void setConnections( )
      > |
      > | {
      > |
      > | String ConnectionStrin g = "Data Source=TestServ er; Integrated
      > Security=SSPI;
      > | Initial Catalog=master" ;
      > |
      > | SqlConn.Connect ionString = ConnectionStrin g;
      > |
      > | SqlConn.Open();
      > |
      > | }
      > |
      > | public DataSet getEmployees(st ring CompanyID)
      > |
      > | {
      > |
      > |
      > | string strSelectSql = "SELECT FirstName, LastName FROM [" + CompanyID +
      > | "].[dbo].[Employee]";
      > |
      > | SqlCommand selectSqlComman d = new SqlCommand(strS electSql, SqlConn);
      > |
      > | SqlDataAdapter sqlData = new SqlDataAdapter( selectSqlComman d);
      > |
      > | DataSet dsSelectData = new DataSet();
      > |
      > | sqlData.Fill(ds SelectData);
      > |
      > | return dsSelectData;
      > |
      > | }
      > |
      > | }
      > |
      > | }
      > |
      > |
      > |
      > |
      > |
      > | Initialization:
      > |
      > | DataClass DC = new DataClass();
      > |
      > | DC.setConnectio ns();
      > |
      > | 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.
      >
      >
      >[/color]


      Comment

      • RSH

        #4
        Re: SQL Server security exception

        System.Security .SecurityExcept ion was unhandled
        Message="Reques t for the permission of type
        'System.Data.Sq lClient.SqlClie ntPermission, System.Data, Version=2.0.0.0 ,
        Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed."
        Source="mscorli b"
        StackTrace:
        at System.Security .CodeAccessSecu rityEngine.Chec k(Object demand,
        StackCrawlMark& stackMark, Boolean isPermSet)
        at System.Security .PermissionSet. Demand()
        at System.Data.Com mon.DbConnectio nOptions.Demand Permission()
        at System.Data.Sql Client.SqlConne ction.Permissio nDemand()
        at
        System.Data.Sql Client.SqlConne ctionFactory.Pe rmissionDemand( DbConnection
        outerConnection )
        at
        System.Data.Pro viderBase.DbCon nectionClosed.O penConnection(D bConnection
        outerConnection , DbConnectionFac tory connectionFacto ry)
        at System.Data.Sql Client.SqlConne ction.Open()
        at GeneralTesting. DataClass.setCo nnections()
        at GeneralTesting. Program.Main()





        "RSH" <way_beyond_oop s@yahoo.com> wrote in message
        news:OWhfyBWFGH A.3036@tk2msftn gp13.phx.gbl...[color=blue]
        >
        > 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.Sq lClient.SqlClie ntPermission, System.Data, Version=2.0.0.0 ,
        > Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed."
        >
        > I have used this connection string before with no problems...what am I
        > overlooking here?
        >
        > Thanks,
        > Ron
        >
        >
        > using System;
        >
        > using System.Collecti ons.Generic;
        >
        > using System.Text;
        >
        > using System.Data.Sql Client;
        >
        > using System.Data;
        >
        > namespace GeneralTesting
        >
        > {
        >
        > class DataClass
        >
        > {
        >
        > public SqlConnection SqlConn = new SqlConnection() ;
        >
        > public void setConnections( )
        >
        > {
        >
        > String ConnectionStrin g = "Data Source=TestServ er; Integrated
        > Security=SSPI; Initial Catalog=master" ;
        >
        > SqlConn.Connect ionString = ConnectionStrin g;
        >
        > SqlConn.Open();
        >
        > }
        >
        > public DataSet getEmployees(st ring CompanyID)
        >
        > {
        >
        >
        > string strSelectSql = "SELECT FirstName, LastName FROM [" + CompanyID +
        > "].[dbo].[Employee]";
        >
        > SqlCommand selectSqlComman d = new SqlCommand(strS electSql, SqlConn);
        >
        > SqlDataAdapter sqlData = new SqlDataAdapter( selectSqlComman d);
        >
        > DataSet dsSelectData = new DataSet();
        >
        > sqlData.Fill(ds SelectData);
        >
        > return dsSelectData;
        >
        > }
        >
        > }
        >
        > }
        >
        >
        >
        >
        >
        > Initialization:
        >
        > DataClass DC = new DataClass();
        >
        > DC.setConnectio ns();
        >
        > DataSet DS = DC.getEmployees ("00010145") ;
        >
        >
        >
        >
        >
        >[/color]


        Comment

        • RSH

          #5
          Re: SQL Server security exception

          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



          "RSH" <way_beyond_oop s@yahoo.com> wrote in message
          news:OWhfyBWFGH A.3036@tk2msftn gp13.phx.gbl...[color=blue]
          >
          > 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.Sq lClient.SqlClie ntPermission, System.Data, Version=2.0.0.0 ,
          > Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed."
          >
          > I have used this connection string before with no problems...what am I
          > overlooking here?
          >
          > Thanks,
          > Ron
          >
          >
          > using System;
          >
          > using System.Collecti ons.Generic;
          >
          > using System.Text;
          >
          > using System.Data.Sql Client;
          >
          > using System.Data;
          >
          > namespace GeneralTesting
          >
          > {
          >
          > class DataClass
          >
          > {
          >
          > public SqlConnection SqlConn = new SqlConnection() ;
          >
          > public void setConnections( )
          >
          > {
          >
          > String ConnectionStrin g = "Data Source=TestServ er; Integrated
          > Security=SSPI; Initial Catalog=master" ;
          >
          > SqlConn.Connect ionString = ConnectionStrin g;
          >
          > SqlConn.Open();
          >
          > }
          >
          > public DataSet getEmployees(st ring CompanyID)
          >
          > {
          >
          >
          > string strSelectSql = "SELECT FirstName, LastName FROM [" + CompanyID +
          > "].[dbo].[Employee]";
          >
          > SqlCommand selectSqlComman d = new SqlCommand(strS electSql, SqlConn);
          >
          > SqlDataAdapter sqlData = new SqlDataAdapter( selectSqlComman d);
          >
          > DataSet dsSelectData = new DataSet();
          >
          > sqlData.Fill(ds SelectData);
          >
          > return dsSelectData;
          >
          > }
          >
          > }
          >
          > }
          >
          >
          >
          >
          >
          > Initialization:
          >
          > DataClass DC = new DataClass();
          >
          > DC.setConnectio ns();
          >
          > DataSet DS = DC.getEmployees ("00010145") ;
          >
          >
          >
          >
          >
          >[/color]


          Comment

          • Willy Denoyette [MVP]

            #6
            Re: SQL Server security exception

            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.

            "RSH" <way_beyond_oop s@yahoo.com> wrote in message
            news:%23y27D%23 WFGHA.3900@TK2M SFTNGP10.phx.gb l...
            |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
            |
            |
            |
            | "RSH" <way_beyond_oop s@yahoo.com> wrote in message
            | news:OWhfyBWFGH A.3036@tk2msftn gp13.phx.gbl...
            | >
            | > 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.Sq lClient.SqlClie ntPermission, System.Data,
            Version=2.0.0.0 ,
            | > Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed."
            | >
            | > I have used this connection string before with no problems...what am I
            | > overlooking here?
            | >
            | > Thanks,
            | > Ron
            | >
            | >
            | > using System;
            | >
            | > using System.Collecti ons.Generic;
            | >
            | > using System.Text;
            | >
            | > using System.Data.Sql Client;
            | >
            | > using System.Data;
            | >
            | > namespace GeneralTesting
            | >
            | > {
            | >
            | > class DataClass
            | >
            | > {
            | >
            | > public SqlConnection SqlConn = new SqlConnection() ;
            | >
            | > public void setConnections( )
            | >
            | > {
            | >
            | > String ConnectionStrin g = "Data Source=TestServ er; Integrated
            | > Security=SSPI; Initial Catalog=master" ;
            | >
            | > SqlConn.Connect ionString = ConnectionStrin g;
            | >
            | > SqlConn.Open();
            | >
            | > }
            | >
            | > public DataSet getEmployees(st ring CompanyID)
            | >
            | > {
            | >
            | >
            | > string strSelectSql = "SELECT FirstName, LastName FROM [" + CompanyID +
            | > "].[dbo].[Employee]";
            | >
            | > SqlCommand selectSqlComman d = new SqlCommand(strS electSql, SqlConn);
            | >
            | > SqlDataAdapter sqlData = new SqlDataAdapter( selectSqlComman d);
            | >
            | > DataSet dsSelectData = new DataSet();
            | >
            | > sqlData.Fill(ds SelectData);
            | >
            | > return dsSelectData;
            | >
            | > }
            | >
            | > }
            | >
            | > }
            | >
            | >
            | >
            | >
            | >
            | > Initialization:
            | >
            | > DataClass DC = new DataClass();
            | >
            | > DC.setConnectio ns();
            | >
            | > DataSet DS = DC.getEmployees ("00010145") ;
            | >
            | >
            | >
            | >
            | >
            | >
            |
            |


            Comment

            • RSH

              #7
              Re: SQL Server security exception

              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 [MVP]" <willy.denoyett e@telenet.be> wrote in message
              news:%23FBJWJXF GHA.1288@TK2MSF TNGP09.phx.gbl. ..[color=blue]
              > 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.
              >
              > "RSH" <way_beyond_oop s@yahoo.com> wrote in message
              > news:%23y27D%23 WFGHA.3900@TK2M SFTNGP10.phx.gb l...
              > |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
              > |
              > |
              > |
              > | "RSH" <way_beyond_oop s@yahoo.com> wrote in message
              > | news:OWhfyBWFGH A.3036@tk2msftn gp13.phx.gbl...
              > | >
              > | > 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.Sq lClient.SqlClie ntPermission, System.Data,
              > Version=2.0.0.0 ,
              > | > Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed."
              > | >
              > | > I have used this connection string before with no problems...what am I
              > | > overlooking here?
              > | >
              > | > Thanks,
              > | > Ron
              > | >
              > | >
              > | > using System;
              > | >
              > | > using System.Collecti ons.Generic;
              > | >
              > | > using System.Text;
              > | >
              > | > using System.Data.Sql Client;
              > | >
              > | > using System.Data;
              > | >
              > | > namespace GeneralTesting
              > | >
              > | > {
              > | >
              > | > class DataClass
              > | >
              > | > {
              > | >
              > | > public SqlConnection SqlConn = new SqlConnection() ;
              > | >
              > | > public void setConnections( )
              > | >
              > | > {
              > | >
              > | > String ConnectionStrin g = "Data Source=TestServ er; Integrated
              > | > Security=SSPI; Initial Catalog=master" ;
              > | >
              > | > SqlConn.Connect ionString = ConnectionStrin g;
              > | >
              > | > SqlConn.Open();
              > | >
              > | > }
              > | >
              > | > public DataSet getEmployees(st ring CompanyID)
              > | >
              > | > {
              > | >
              > | >
              > | > string strSelectSql = "SELECT FirstName, LastName FROM [" + CompanyID
              > +
              > | > "].[dbo].[Employee]";
              > | >
              > | > SqlCommand selectSqlComman d = new SqlCommand(strS electSql, SqlConn);
              > | >
              > | > SqlDataAdapter sqlData = new SqlDataAdapter( selectSqlComman d);
              > | >
              > | > DataSet dsSelectData = new DataSet();
              > | >
              > | > sqlData.Fill(ds SelectData);
              > | >
              > | > return dsSelectData;
              > | >
              > | > }
              > | >
              > | > }
              > | >
              > | > }
              > | >
              > | >
              > | >
              > | >
              > | >
              > | > Initialization:
              > | >
              > | > DataClass DC = new DataClass();
              > | >
              > | > DC.setConnectio ns();
              > | >
              > | > DataSet DS = DC.getEmployees ("00010145") ;
              > | >
              > | >
              > | >
              > | >
              > | >
              > | >
              > |
              > |
              >
              >[/color]


              Comment

              Working...