【发布时间】:2011-02-19 12:13:49
【问题描述】:
我正在开发 web 服务。我想维护状态信息,以便只有在登录后才能访问所有 WebMethods。 我试过但遇到问题。 我附上我的代码。 任何其他替代方案也将受到欢迎。
[
WebService(Namespace = "http://amSubfah.org/")]
[
WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 要允许使用 ASP.NET AJAX 从脚本调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
公开
类登录:System.Web.Services.WebService {
消息 msgObj = new Message();
BaseClass b = new BaseClass();
PasswordEncryptionDecryption pedObj = new PasswordEncryptionDecryption();
public AuthHeader Authentication=new AuthHeader();
公共登录(){
//如果使用设计的组件,请取消注释以下行
//初始化组件();
}
[
SoapHeader("身份验证", 必需 = true)]
[System.Web.Services.
WebMethod(EnableSession = true)]
public string checkUserLogin(string user, string pwd)
{
数据集 dsLogin = new DataSet();
列表 sqlParams = new List();
SqlParameter sqlParam1 = new SqlParameter("@UserName", SqlDbType.NVarChar);
sqlParam1.Value = 用户;
sqlParams.Add(sqlParam1);
SqlParameter sqlParam2 = new SqlParameter("@Password", SqlDbType.NVarChar);
string pass = pedObj.encryptPassword(pwd);
sqlParam2.Value = 通过;
sqlParams.Add(sqlParam2);
试试
{
b.initializeDBConnection();
dsLogin = b.execSelectLoginQuery(
Query.strSelectLoginData, sqlParams);
}
捕获(SqlException sqlEx)
{
字符串 str = msgObj.msgErrorMessage + sqlEx.Message + sqlEx.StackTrace;
}
{if ((dsLogin != null) && (dsLogin.Tables[0].Rows.Count != 0))
{
会话[
“用户名”] = 用户;
字符串 sessionId = System.Guid.NewGuid().ToString();
Authentication.sessionId = sessionId;
Authentication.Username = 用户;
返回 msgObj.msgLoginSuccess;
}
否则
返回 msgObj .msgLoginFail ;
}
//web注册方法
[
SoapHeader("身份验证", 必需 = true)]
[系统.Web.服务。
WebMethod (EnableSession =true)]
公共字符串 insertRegistrationDetails(string fName,string lName,string email,string pwd)
{
//string u = Session["username"].ToString();
//如果 (u == "")
//{
// //checkUserLogin(fName,pwd );
// return "请先登录";
//}
if (Authentication.Username == null || Authentication.sessionId == null)
{
return "请先登录";
}
列表 sqlParams = new List();
int 插入 = 0;
字符串味精 = "" ;
SqlParameter sqlParam = new SqlParameter("@FName", SqlDbType.NVarChar);
sqlParam.Value = fName;
sqlParam.Size = 50;
sqlParams.Add(sqlParam);
SqlParameter sqlParam1 = new SqlParameter("@LName", SqlDbType.NVarChar);
sqlParam1.Value = lName;
sqlParam1.Size = 50;
sqlParams.Add(sqlParam1);
SqlParameter sqlParam5 = new SqlParameter("@Email", SqlDbType.NVarChar);
sqlParam5.Value = 电子邮件;
sqlParam5.Size = 50;
sqlParams.Add(sqlParam5);
SqlParameter sqlParam7 = new SqlParameter("@Password", SqlDbType.NVarChar);
sqlParam7.Value = pedObj .encryptPassword (pwd);
sqlParam7.Size = 50;
sqlParams.Add(sqlParam7);
试试
{
b.initializeDBConnection();
插入 = b.execByKeyParams(
Query.strInsertIntoRegistrationTable1, sqlParams);
如果(插入!=0)
{
msg = msgObj .msgRecInsertedSuccess ;
}
}
捕获(SqlException sqlEx)
{
字符串 str = msgObj.msgErrorMessage + sqlEx.Message + sqlEx.StackTrace;
}
返回消息;
}
公共类 AuthHeader : SoapHeader
{
公共字符串用户名;
公共字符串 sessionId;
}
}
【问题讨论】: