www.pudn.com > userapp.rar > UserService.cs


namespace Cjjer{ 
using System; 
using System.Web.Services; 
using System.Xml; 
using System.Xml.XPath; 
[WebService (Name="WebsCjjer",Description="一个验证用户登陆的web服务",Namespace="http://www.cjjer.com/webs/")] 
public class UserService{ 
	[WebMethod (Description ="输入用户名和密,返回权限Int值,0表示失败",MessageName="Login")] 
	public int Login(string UserName,string UserPassword){ 
		return User.Check(UserName,UserPassword); 
	} 
 
	[WebMethod (Description ="新增用户名和密码,返回结果,0表示失败",MessageName="AddNew")] 
	public int AddNew(string UserName,string UserPassword){ 
		if(User.Check(UserName,UserPassword)!=0) return 0; 
		return 1; 
	} 
 
}; 
public class User{ 
 
	const string _XmlUserData ="data\\user.xml"; 
	public static string AppPath{ 
		get{ 
			string tempstr; 
			try{ 
				tempstr = System.Web.HttpContext.Current.Server.MapPath(@"~"); 
			}catch(System.Exception){//屏蔽.NET 版本不同时的错误信息 
				tempstr = System.Web.HttpContext.Current.Server.MapPath(@"."); 
			} 
			return tempstr+"\\"; 
		} 
	} 
	public static string XmlUserData{ 
		get{return AppPath+_XmlUserData;} 
	} 
	public static int Check(string username,string userpassword){ 
		int int_Re = 0; 
		try{ 
			string xpath = @"descendant::user[name='"+username+"' and password='"+CodeString(userpassword)+"']/level"; 
			int_Re= System.Convert.ToInt32(SelectSingleNodeValue(XmlUserData,xpath)); 
		}catch(System.Exception){ 
			int_Re = 0; 
		} 
		return int_Re; 
	} 
	public static string  CodeString(string args){ 
		return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(args, "Md5");//SHA1 
	} 
	public static string SelectSingleNodeValue(string FullXmlPath,string XPathString){ 
		string returnstr =null; 
		XmlDocument doc = new XmlDocument(); 
		try{ 
			doc.Load(FullXmlPath); 
			XmlNode objNode = doc.SelectSingleNode(XPathString); 
			returnstr = objNode.LastChild.InnerText; 
		}catch(Exception){} 
		return returnstr; 
	} 
}; 
}