Impersonation code

Discussion in 'ASP .Net' started by Pramod, May 12, 2005.

  1. Pramod

    Pramod Guest

    Hi,


    I have the following Impersonation code. This code is in C# and I want
    to retrieve the data from the SQL server database using the Specified
    windows username and password.


    public bool ImpersonateUser(string sUsername, string sDomain, string
    sPassword)
    {
    bool bImpersonated = false;
    pExistingTokenHandle = new IntPtr(0);
    pExistingTokenHandle = IntPtr.Zero;


    try
    {
    const int LOGON32_PROVIDER_DEFAULT = 0;


    // create token
    //const int LOGON32_LOGON_INTERACTIVE = 2;


    const int LOGON32_LOGON_NETWORK = 3;
    //const int SecurityImpersonation = 2;


    // get handle to token
    bImpersonated = LogonUser(sUsername, sDomain, sPassword,
    LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
    ref pExistingTokenHandle);
    }
    catch (Exception e)
    {
    ATrace.WriteLine( ATrace.CATEGORY.ERROR,
    "SQLQueryBuilder::ImpersonateU­­ser - " + e.Message );
    }
    return bImpersonated;
    }

    This code does not throw any exception but returns false.

    Interesting thing is, I have created a sample application similar to
    this in ASP.Net. The same application works but not mine which is ther
    in project.


    So, is ther any prequisite to run that code ???
    Or is ther any project settings ???


    If you know, then plz let me know ASAP


    Thanks,
    Pramod.
    Pramod, May 12, 2005
    #1
    1. Advertising

  2. Pramod

    TE Guest

    Hi,

    I have not checked your code, but as far as I know the executing user of the
    LogonUser API needs to be granted the "Act as part of OS" right. (I think
    this is a Windows2000 Issue only).

    Regards

    Tom





    "Pramod" <> schrieb im Newsbeitrag
    news:%...
    > Hi,
    >
    >
    > I have the following Impersonation code. This code is in C# and I want
    > to retrieve the data from the SQL server database using the Specified
    > windows username and password.
    >
    >
    > public bool ImpersonateUser(string sUsername, string sDomain, string
    > sPassword)
    > {
    > bool bImpersonated = false;
    > pExistingTokenHandle = new IntPtr(0);
    > pExistingTokenHandle = IntPtr.Zero;
    >
    >
    > try
    > {
    > const int LOGON32_PROVIDER_DEFAULT = 0;
    >
    >
    > // create token
    > //const int LOGON32_LOGON_INTERACTIVE = 2;
    >
    >
    > const int LOGON32_LOGON_NETWORK = 3;
    > //const int SecurityImpersonation = 2;
    >
    >
    > // get handle to token
    > bImpersonated = LogonUser(sUsername, sDomain, sPassword,
    > LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
    > ref pExistingTokenHandle);
    > }
    > catch (Exception e)
    > {
    > ATrace.WriteLine( ATrace.CATEGORY.ERROR,
    > "SQLQueryBuilder::ImpersonateU­­ser - " + e.Message );
    > }
    > return bImpersonated;
    > }
    >
    > This code does not throw any exception but returns false.
    >
    > Interesting thing is, I have created a sample application similar to
    > this in ASP.Net. The same application works but not mine which is ther
    > in project.
    >
    >
    > So, is ther any prequisite to run that code ???
    > Or is ther any project settings ???
    >
    >
    > If you know, then plz let me know ASAP
    >
    >
    > Thanks,
    > Pramod.
    >
    >
    >
    >
    >
    TE, May 12, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelly D. Jones

    Problem with impersonation and delegation

    Kelly D. Jones, Sep 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,819
  2. Raymond Basque

    Re: ASP.NET Fails after SP4 with Impersonation

    Raymond Basque, Jun 27, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    519
  3. Wim Hollebrandse
    Replies:
    0
    Views:
    831
    Wim Hollebrandse
    Jun 11, 2004
  4. =?Utf-8?B?c21IYWln?=

    using impersonation in code

    =?Utf-8?B?c21IYWln?=, Aug 6, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    413
    =?Utf-8?B?c21IYWln?=
    Aug 7, 2005
  5. Seth Williams

    Impersonation in code/Sharepoint

    Seth Williams, May 20, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    568
    Seth Williams
    May 21, 2008
Loading...

Share This Page