How to provide authentication in WebService. I often see how to solve the post when WebService does not allow anonymous access. I tried it myself. In fact, this is difficult for this, as long as the second three sentences can be resolved.
I built a test for a test, I didn't write any code. I only had a default method of the project HelloWorld (I originally commented. I canceled it). Then remove this webservice anonymous access, and The option to integrate Windows is also canceled.
Create a Winform project, I call the WebService project in this project, add a web reference in the reference, these and normal steps. Here are, we only need to modify the agent class just generated. Add identity information in its constructor.
Below is an agent class that is automatically generated by the development tool.
/ / -------------------------------------------------------------------------------------------- ------------------------------ //
// // This source code is automatically generated by Microsoft.vsdesigner 1.1.4322.2032. // namespace Test.localhost {using System.Diagnostics; using System.Xml.Serialization; using System; using System.Web.Services.Protocols; using System.ComponentModel; using System.Web.Services; using System.Security; using System .Net; ///
/ / This is an identity information to access the WebService. NetworkCredential myCred = new NetworkCredential ( "Administrator", "password", "greystar.com"); CredentialCache myCache = new CredentialCache (); myCache.Add (new Uri (this.Url), "Basic", myCred); // request verification this.Credentials = myCache;} ///
/ / This is an identity information to access the WebService. NetworkCredential myCred = new NetworkCredential ( "Administrator", "password", "greystar.com"); CredentialCache myCache = new CredentialCache (); myCache.Add (new Uri (this.Url), "Basic", myCred); // Request trial this.credentials = mycache;
At this time, you can try it with you, it is not successful. For other WEB requests, if authentication is required, you can also use the above method.