views:

47

answers:

1

I'm trying to use SOAP headers to allow for SQL Authentication while accessing a webservice published on my SQL 2005 box via HTTP Endpoint. On the endpoint, I've set Authentication = (Basic), Ports = (SSL), and LOGIN_TYPE = MIXED. I'm able to generate the WSDL and consume it just fine in VS utilizing domain credentials. However, when I try to implement SOAP headers to allow for SQL Authentication, i'm running into problems. I've followed MS BOL to the letter (http://msdn.microsoft.com/en-us/library/ms189619(SQL.90).aspx), but for some reason, i'm not sending the SOAP header. I've verified this by using fiddler (http://www.fiddler2.com/fiddler2/) to trap my https messages and look at them. Any help would be greatly appreciated. Included is the code I've been using (the names have been changed to protect the innocent)

namespace ConsoleApplication.WebService
{
class Program
{
    static void Main(string[] args)
    {
        //Prevents error due to self signed cert
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

        Stuff stuff = new Stuff();

        stuff.DoSomthing();
    }
}

public class Stuff
{
    [System.Web.Services.Protocols.SoapHeaderAttribute("sqlSecurity")]
    public int DoSomthing()
    {
        Webservice ws = new Webservice();

        CredentialCache myCreds = new CredentialCache();
        myCreds.Add(new Uri(ws.Url), "Basic", new NetworkCredential("netaccount", "netpass", "domain"));
        ws.Credentials = myCreds;

        ws.sqlSecurity = new SqlSoapHeader.Security();
        ws.sqlSecurity.Username = "sqluser";
        ws.sqlSecurity.Password = "sqlpass";

        try
        {
            ws.SelectUserAccountByUserName("someuser");
        }
        catch (SoapException ex)
        {

            string txterror = ex.Detail.InnerText;
            return 0;
        }

        return 1;
    }
}

public partial class Webservice
{
    public SqlSoapHeader.Security sqlSecurity;
}
}

This code utilizes the SqlSoapHeader class as documented in the BOL reference from above.

I error at calling ws.SelectUserAccountByUserName() with an "Execute permission denied" due to the fact that the "netaccount" user doesn't have rights to execute the stored proc. But again, this is because according to the soap message, no header with the sqluser info is being passed.

+3  A: 

The SoapHeaderAttribute needs to be added to the web method. It won't do you any good to add it to some arbitrary calling method.

Aaronaught
This is great! Chock it up to me being a newbie programmer. Now a bigger question is how do I prevent this from being overwritten every time I update the Web Reference? It's going to be very time consuming and annoying to have to add dozens of attributes every time the wsdl changes.
Leifab
@Leifab: If you have control over the WSDL then you can instruct the code-gen to add it automatically; I'm not intimately familiar with SQL Server's Native XML Web Services, but it doesn't look like you have much control over the metadata it generates, so I'm afraid that you may be out of luck. In the past I used WSE extensions to affect the generated code, but using plain WebServices I'm not sure how to go about that (or if it's even possible). Wish I could tell you more...
Aaronaught