Implement Window Authentication in WebService

xiaoxiao2021-03-06  20

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 code WAS generated by a Tool.// runtime version: 1.1.4322.2032/// CHANGES TO this file may Cause Incorrect Behavior and Will Be Lost IF // The code is regenerated> // --------------- -------------------------------------------------- -------------

// // 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; /// [System.Diagnostics.DebuggerStepThroughAttribute ()] [System.ComponentModel.DesignerCategoryAttribute ( "code")] [System.Web.Services.WebServiceBindingAttribute (Name = "Service1Soap", Namespace = "http : //tempuri.org/ ")] public class Service1: System.Web.Services.Protocols.SoapHttpClientProtocol {/// public Service1 () {this.Url =" http: // localhost / WebServiceTest / Service1 .asmx ";

/ / 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;} /// [System.Web.Services.Protocols.SoapDocumentMethodAttribute ( "http://tempuri.org/HelloWorld", RequestNamespace = "http://tempuri.org/ ", ResponseNamespace =" http://tempuri.org/ ", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] public string HelloWorld () { Object [] results = this.invoke ("HelloWorld", New Object [0]); Return (String));} /// Public System.iasyncResult BeginHelloWorld (System.asyncCallback Callback, Object asyncState) {Return this.beginInvoke ("HelloWorld", New Object [0], Callback, AsyncState;} /// PUB LiC String endhelloworld (system.iasyncResult asyncRESULT) {Object [] results = this.endinvoke (asyncRESULT); RETURN (String));}}}

/ / 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.

转载请注明原文地址:https://www.9cbs.com/read-43745.html

New Post(0)