The ability to implement window authentication in WebService

Source: Internet
Author: User
Tags anonymous

How to provide authentication functionality in WebService. I often see posts on the csdn how to resolve when WebService does not allow anonymous access. Try it yourself. In fact, this is not very difficult in the middle, as long as two or three code can be resolved.

I've built a test webservice here that doesn't write any code, only a default method HelloWorld (originally annotated) When building a project. I canceled it. Then cancel the anonymous access option for this webservice and also cancel the option to integrate Windows.

To build a WinForm project, I called the WebService project in this project and added a Web reference to the reference, which is the same as the normal steps. In fact, we just need to modify the proxy class that just arose. Add identity information to its constructor.

The following is the proxy class that the development tools automatically generate.

//------------------------------------------------------------------------------
<autogenerated>
This code is generated by a tool.
Runtime version:1.1.4322.2032
//
Changes to this file may cause incorrect behavior and would be lost if
The code is regenerated.
</autogenerated>
//------------------------------------------------------------------------------

//
This source code is automatically generated by the Microsoft.vsdesigner 1.1.4322.2032 version.
//
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;

<remarks/>
[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 {

<remarks/>
Public Service1 () {
This. URL = "Http://localhost/WebServiceTest/Service1.asmx";

//This is the identity information to access the WebService.
   networkcredential mycred = new NetworkCredential ("Administrator", "Password", "greystar.com");
   credentialcache Mycache = new CredentialCache ();
   mycache.add The new Uri (this. URL), "Basic", mycred, and/or requires validation of the
   this. Credentials=mycache;
       }
       
       ///<remarks/
        [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) (Results[0]);
}

<remarks/>
Public System.IAsyncResult Beginhelloworld (System.AsyncCallback callback, object asyncstate) {
return this. BeginInvoke ("HelloWorld", new Object[0], callback, asyncstate);
}

<remarks/>
public string Endhelloworld (System.IAsyncResult asyncresult) {
Object[] results = this. EndInvoke (asyncresult);
Return ((String) (Results[0]);
}
}
}

Here is the identity information to access the WebService.
NetworkCredential mycred = new NetworkCredential ("Administrator", "Password", "greystar.com");
CredentialCache Mycache = new CredentialCache ();
Mycache.add the new Uri (this. URL), "Basic", mycred, and/or require validation
This. Credentials=mycache;

At this point recompile you can try, is not successful. For other forms of Web requests, if authentication is required, you can also use the above method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.