完全适用ASP.NET的认证机制 –可以使用FormsAuthentication •WebService方法可以操作Cookie –Impersonation –PrincipalPermission WebService7.cs Code using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.Web.Script.Services; /// <summary> /// WebService7 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService] public class WebService7 : System.Web.Services.WebService { public WebService7 () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string HelloWorld() { if (!HttpContext.Current.User.Identity.IsAuthenticated) { throw new ApplicationException("please Login In"); } return "Hello "+HttpContext.Current.User.Identity.Name; } } Default8.aspx Code: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager runat=server ID="aaa" ScriptMode=Debug> <Services> <asp:ServiceReference Path="WebService7.asmx" /> </Services> </asp:ScriptManager> <mce:script language=javascript type="text/javascript"><!-- function GetMessage() { WebService7.HelloWorld(getresult); } function getresult(result) { alert(result); } // --></mce:script> <input id="Button1" type="button" value="button" οnclick="GetMessage()" /> </form> </body> </html> Default8.aspx.cs Code: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default8 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { FormsAuthentication.SetAuthCookie("churenyouzi", false); } } Web.config Add Code: <authentication mode="Forms"></authentication> 参考:ASP.NET AJAX深入浅出系列课程