locked
Application impersonation in c# RRS feed

  • Question

  • Hi,

    I have a windows form application in c# that needs to impersonate another applications login to sql server. I request login credentials from the user. Can someone please help me with code on how to impersonate this existing application's login to the sql database as i need to retrieve data from the database?

    Thanks in advance

    Camren

    Wednesday, September 19, 2012 1:10 PM

Answers

  • needs to impersonate another applications login to sql server.

    Hello Camren,

    What for a authentification mode is use to logon to SQL Server; Windows or SQL Login? If it's a SQL login, then you simple have to pass the credential with the connection string.

    Otherwise you have to impersonate the security context of the C# application, means switching to that different Windows account.


    Olaf Helper

    Blog Xing

    • Marked as answer by Camren19 Thursday, September 20, 2012 6:34 AM
    Wednesday, September 19, 2012 4:01 PM
  • I am using sql login...

    Hello Camren,

    Then it's really easy: See http://www.connectionstrings.com/sql-server-2008 how a connection string with SQL login should look like, e.g.

    Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;


    Olaf Helper

    Blog Xing

    • Proposed as answer by Allen Li - MSFT Thursday, September 20, 2012 1:45 AM
    • Marked as answer by Camren19 Thursday, September 20, 2012 6:34 AM
    Wednesday, September 19, 2012 5:45 PM

All replies

  • You want to impersonate application A from application B, right?

    if YES, Do you have the code of application A? You can do it by using a file, registry or something similar to change the credentials of application A from application B. But this would require you to change application A code.

    Wednesday, September 19, 2012 2:20 PM
  • needs to impersonate another applications login to sql server.

    Hello Camren,

    What for a authentification mode is use to logon to SQL Server; Windows or SQL Login? If it's a SQL login, then you simple have to pass the credential with the connection string.

    Otherwise you have to impersonate the security context of the C# application, means switching to that different Windows account.


    Olaf Helper

    Blog Xing

    • Marked as answer by Camren19 Thursday, September 20, 2012 6:34 AM
    Wednesday, September 19, 2012 4:01 PM
  • I am using sql login... so what credentials should I pass through the connection string?

    Camren Chetty

    Wednesday, September 19, 2012 5:42 PM
  • I am using sql login...

    Hello Camren,

    Then it's really easy: See http://www.connectionstrings.com/sql-server-2008 how a connection string with SQL login should look like, e.g.

    Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;


    Olaf Helper

    Blog Xing

    • Proposed as answer by Allen Li - MSFT Thursday, September 20, 2012 1:45 AM
    • Marked as answer by Camren19 Thursday, September 20, 2012 6:34 AM
    Wednesday, September 19, 2012 5:45 PM
  • Hi its working now, I just impersonated the user within the database using something like this:

    create procedure test

    with exec as 'User_Name'

    as

    select.....

    from....

    thanks


    Camren Chetty

    Thursday, September 20, 2012 6:36 AM