Webservice is called for authentication and webservice authentication.

Source: Internet
Author: User

Webservice is called for authentication and webservice authentication.

My colleague said that the authentication window will pop up when webservice is called, and 401 will be returned for direct calls because authentication is set during site deployment (the account name is configured by myself ). Therefore, the authentication credential must be added during the call.

How can I obtain the creden for identity authentication? You can use the NetworkCredential (The namespace is System. Net) provided by Microsoft. The Code is as follows.

Using System; using System. net; using webserviceSync. helloWebservice; namespace webserviceSync {public partial class WebForm1: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {MySoapHeader my = new MySoapHeader (); my. userName = ""; Response. write (webserviceClient. helloWorld (my ));} /// <summary> /// obtain the network credential /// </summary> /// <returns> </returns> public static NetworkCredential GetCredential () {// return new NetworkCredential ("Administrator", "888888", "192.168.2.58"); // return (NetworkCredential) CredentialCache directly. defaultCredentials; // set local creden }/// <summary> /// verify whether the creden pass. /// </summary> /// <returns> </returns> public static bool IsVerifySucess () {string url =" http://192.168.2.58:7777/WebService.asmx?wsdl "; WebResponse webResponse = null; try {WebRequest webRequest = WebRequest. create (url); webRequest. credentials = GetCredential (); webRequest. timeout = 15000; webResponse = webRequest. getResponse ();} catch (Exception ex) {throw ex;} return true ;} /// <summary> /// attributes /// </summary> private static WebServiceSoapClient _ webserviceClient = null; /// <summary> /// webservice // </summary> public static WebServiceSoapClient webserviceClient {get {if (_ webserviceClient = null) {bool isSucess = IsVerifySucess (); if (isSucess = true) {_ webserviceClient = new WebServiceSoapClient (); _ webserviceClient. clientCredentials. windows. clientCredential = GetCredential (); // This property ........}} return _ webserviceClient ;}}}}

How to Set creden locally refer to http://jingyan.baidu.com/article/a3aad71aaa32eeb1fb0096c8.html

 

Related Article

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.