Reputation:
I am new to .NET and C#. I created a Web service, and I am able to view it from a Web page. When I try to call it from a Windows Application, I get the Exception 401 : unauthorized. Code compiles OK, but throws exception when running. This is the code from the Windows App. :
namespace BookStore
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Create a new instance of a service
localhost.Service1 datasvc = new localhost.Service1();
// Create instance of dataset, using WebService method GetTitleAuthors.
DataSet myData = datasvc.GetTitleAuthors();
// Set DataGrid's datasource to the myData DataSet.
dataGridView1.DataSource = myData;
//Expand all rows.
//dataGridView1.Expand(-1);
//Expand DataTable
//dataGridView1.NavigateTo(0, "Authors");
}
}
}
PS : I am using Windows Authentication in the website that hosts the web service.
Upvotes: 0
Views: 4181
Reputation: 355
I don't know what type your Service1 object inherits so I can't say what properties or methods you have associated with it, but whenever I know you can make calls to you web service with using
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
And then either using
req.UseDefaultCredentials = true;
or
req.Credentials = new NetworkCredential(userName, password);
Upvotes: 1
Reputation: 82325
I believe there is a property on the generated proxy to the effect of UseDefaultCredentials
try setting that to true.
datasvc.UseDefaultCredentials = true;
Although it's been a while I think this will force the service to pass windows credentials.
Upvotes: 1