locked
WCF Impersonation RRS feed

  • Question

  • Hi,

    Not sure if this is the right section to post my question.

    I'm having a problem with the WCF Impersonation.

    What I want it to do is to allow WCF to impersonate a AD user defined in the config file and use it to call a stored proc in the database.

    So far what I did was:
    I use the wsHttpBinding.

    Add the following lines in the web.config:
    Code Snippet

    <system.web>
    <identity impersonate="true" userName="ADTestUser" password="MyT3stuS3r"/>

    </system.web>


    The sql connection string is something like this:
    Code Snippet

    <add name="sql" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=True"/>


    I called the stored proc like this:
    Code Snippet

     List<string> result = new List<string>();

    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ConnectionString))

    {
    using (SqlCommand cmd = new SqlCommand("", con))
    {
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.CommandText = "SP_procMyTest";
    cmd.Connection.Open();

    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
    result.Add(reader["id"].ToString());
    }
    }
    }
    }


    When the application is trying to call the stored procedure, it returns the error:
    Login failed for user(null).

    Help would be very appreciated. Thank you.

    Regards,

    ssboyz

    Tuesday, December 2, 2008 11:55 PM

Answers

  • Hi Attiq,

    Thanks for the reply.

    I actually found the solution myself.
    I am using windows xp and iis 5.1. The reason i was unable to impersonate correctly is due to how wcf interacts with iis 5.1.

    What I did is to set the aspnetcompatibilityenabled mode to true to impersonate a specific user.

    Regards,

    ssboyz
    Friday, December 5, 2008 4:48 AM

All replies

  • Hi,

    Try following in behaviour definition:

    <serviceAuthorization impersonateCallerForAllOperations="true"></serviceAuthorization>

    OR you can also set this attribute for specific method by using following:

    [OperationBehavior(Impersonation = ImpersonationOption.Required)]


    Hope it will help.
    Wednesday, December 3, 2008 8:07 PM
  • Hi Attiq,

    Thanks for the reply.

    I actually found the solution myself.
    I am using windows xp and iis 5.1. The reason i was unable to impersonate correctly is due to how wcf interacts with iis 5.1.

    What I did is to set the aspnetcompatibilityenabled mode to true to impersonate a specific user.

    Regards,

    ssboyz
    Friday, December 5, 2008 4:48 AM