Calling ASMX Service from C# app with Windows authentication?

11,764

Solution 1

Are you passing network credentials to your web service object before calling it? Something like:

NetworkCredential credentials = new
NetworkCredential(UserName,SecurelyStroredPassword,Domain);

// Add the NetworkCredential to the CredentialCache.
credentialCache.Add(new Uri(webSrvc.Url), 
                   "Basic", credentials);

Cribbed from: http://msdn.microsoft.com/en-us/library/bfazk0tb.aspx

Basically when the web service is called from the web app you already have all the cookie and session info sticking around. When you write a standalone application it won't have access to the cookies, form data, etc.

Solution 2

Set

UseDefaultCredentials = true; 

This will authenticate the the currentuser (or the account the current process is running)

Share:
11,764
dave823
Author by

dave823

Professional software developer since 2006.

Updated on June 04, 2022

Comments

  • dave823
    dave823 almost 2 years

    I have created simple .NET Web Service (ASMX) that I call from my .NET web application. The service and web app both use Windows Authetication.

    com.mydomain.MyWebService webSrvc = new com.mydomain.MyWebService();
    string output = webSrvc.MyServiceMethod();
    

    I am now creating Windows app with C#, and would like to call the same web service from there. I have added a web reference in my project to the service, and I can make calls to the service web methods without issue. The problem is, inside the web methods, I am trying to retrieve the user's windows user name.

    string username = User.Identity.Name; //always blank
    

    This works when I call the web service from my web app, but when I call it from my windows app, it always returns blank, and User.Identity.IsAuthenticated is always false, if that is relevant. What am I missing here. I just need to get the Windows user username that is running the console app, that called the web service. I have tried setting the credentials of webSrvc to the ClientCache default credentials, but that doesn't solve the problem.

    UPDATE: I don't know if this is useful, but I learned that if I use Environment.UserName, it will give "NETWORK_SERVICE", and then if i enable impersonation, it will give "IUSR_[host_name]". It seems like i'm just missing something really simple. I just want to get the current windows user, who is running the windows app (which calls the web service).

  • dave823
    dave823 almost 12 years
    I am using default credentials since I dont know the password for every user in the company. Shouldn't that work since I am using windows authentication with anonymous access disabled.
  • Richthofen
    Richthofen almost 12 years
    "Shouldn't that work since I am using windows authentication with anonymous access disabled". No, actually. The code you are using does not 'pass through' the credentials to your web service object. It works in the browser because that's a behavior that the browser manufacturer (MS) has turned on. However your windows forms app does not pass the credentials under which it is running to a web service by default. you have to explicitly set it.
  • dave823
    dave823 over 11 years
    Thank you for your comments so far. In your code example, I still don't understand how I am suppose to get a users password to pass in to the network credential. I can get the username and domain from windows identity, but what about password? Also, I notice your code uses "Basic" authentication. Will this work if IIS web app is configured for "Integrated Windows Authentication".
  • Richthofen
    Richthofen over 11 years
    User must type it in. You can't just 'detect' the password. It doesn't work that way. Even Microsoft Outlook requires you to put your Windows Domain Password in (albeit once, and then it 'saves' it.)