在WebService中实现window身份验证的功能

本文介绍了在WEBSERVICE中提供身份验证功能的方法。通过建立测试用的WEBSERVICE并取消匿名访问和集成WINDOWS选项,再建立WINFORM工程调用该WEBSERVICE,修改自动生成的代理类,在构造函数里添加身份信息,重编译即可实现身份验证,此方法也适用于其他要求身份验证的WEB请求。

如何在WEBSERVICE中提供身份验证的功能。我在优快云上常看到当WEBSERVICE不允许匿名访问时如何解决的贴子。自己试了一下。其实这中间的难度不大,只要二三句代码就可以解决。

我这里建一个测试用的WEBSERVICE,里面没有写任何代码,只有建工程时的一个默认的方法HelloWorld(本来是注释的。我把它取消了).然后将此WEBSERVICE的匿名访问的选项取消,并将集成WINDOWS的选项也取消了。

建立一个WINFORM工程,我在这个工程里调用刚才的WEBSERVICE工程,在引用里添加一个WEB引用,这些和正常的步骤是一样的。下面其实我们只要修改一下刚才产生的代理类。在其构造函数里加上身份信息即可。

下面是开发工具自动生成的代理类。

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

//
// 此源代码是由 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;

/// <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";

//这里是访问WEBSERVICE的身份信息。
NetworkCredential myCred = new NetworkCredential("Administrator","password","greystar.com");
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(this.Url), "Basic", myCred);//要求验证
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]));
}
}
}

//这里是访问WEBSERVICE的身份信息。
NetworkCredential myCred = new NetworkCredential("Administrator","password","greystar.com");
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(this.Url), "Basic", myCred);//要求验证
this.Credentials=myCache;

此时重编译一下你可以试一下,是不是成功了。对于其他方式的WEB请求,如果要求身份验证,也可以使用上面的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值