all groups > sql server connect > january 2006 >
You're in the

sql server connect

group:

Problems using SMO to impersonate SQL connection (SQL2k Dev SP4 + .NET 2.0)


Problems using SMO to impersonate SQL connection (SQL2k Dev SP4 + .NET 2.0) Daniel Portal
1/27/2006 4:11:27 PM
sql server connect:
Hi there,

Im trying to use SMO to impersonate a typed (not logged) user, using
Windows Auth to connect SQL Server!

Even though, I'm having many problems while trying to trace the
exception I got!

If you manage to perform a Windows Auth connection to SQL Server using an
inputted user instead of the logged user, please contact me!

If you're just trying to help or learn some more about it, I'm leaving
instructions on how the code works and how to test the connection.


Thanks in advance!


Title: Using SMO to impersonate an inputted user (and not the logged user)
Environment: Have a SQL Server 2k SP4 installed (Either Dev or Ent edition
will do). Have .NET 2.0 installed and, if possible, Visual Studio 2005.

1st: Create an Account in the SQL Server associated with a Domain User
(please, do not use the account you'll be logged in while testing it), and
set the SQL Server Security to Windows Auth Only (Enterprise Manage >> Right
Click SQL Server >> Properties >> Security Tab)

2nd; Create a Windows Application Containing 4 textboxes and 1 button

3rd: Name the texboxes:
1: txtUsername
2: txtPassword
3: txtServerInstance
4: txtDatabase

And the button
1: btnTestConnection

3rd: Add to your project a reference to the
Microsoft.SqlServer.ConnectionInfo assembly

4th: Type the code below in the btnTestConnection onclick event

[code]
---------------------------------------------------------------------------------------------------------------------------------------------
Microsoft.SqlServer.Management.Common.ServerConnection oServerConnection =
new Microsoft.SqlServer.Management.Common.ServerConnection();
// Indicate to connect with impersonate

oServerConnection.ConnectAsUser = true;

oServerConnection.LoginSecure = true;

// Set user and password

oServerConnection.ConnectAsUserName = utxtUsername.Text;

oServerConnection.ConnectAsUserPassword = utxtPassword.Text;

oServerConnection.ServerInstance = txtServeinstance.Text;

oServerConnection.DatabaseName = txtDatabase.Text;

oServerConnection.Connect();

---------------------------------------------------------------------------------------------------------------------------------------------

[/Code]



Run the application and input the informations in the textboxes, but
remember to use the domain-user that you gave access to your SQL Server (and
different from the user you're actually logged in with)

I got to this stage and I don't get the problem. The exception says there's
something wrong with the User / Password, yet I know it's a valid user/pwd
to connect! The documentation of the
Microsoft.SqlServer.Management.Common.ServerConnection (and every of every
class inside the SMO assembly) are poorly documented, which makes a lot
harder to figure what's happening not to work!

Thanks in advance and hope some of you are able to help me.

Regards



Daniel Portal




AddThis Social Bookmark Button