C#使用Mongodb帮助类来注册及登录,欢迎吐槽,肯定有需要修改及优化的地方,不吝赐教
-
#region 登录的模块
protected void butlogin_Click(object sender, EventArgs e)
{
string uname = Request["uname"]; string upswd = Request["upswd"]; string ckme = Request["ckme"];
string script = string.Empty;
if (uname.IsNotEmptOrNull() && upswd.IsNotEmptOrNull())
{
UserInfo info = new UserInfo();
info.name = uname; info.pswd = upswd;
string sessionId = Guid.NewGuid().ToString();//cookid cp2
HttpCookie cp1 = new HttpCookie("cp1", sessionId);
HttpCookie cp2 = new HttpCookie("cp2", uname + "**" + Md5Helper.Md5Str(Md5Helper.Md5Str(upswd)));
DateTime loginDay ;
if (ckme.IsNotEmptOrNull())
{
cp1.Expires = DateTime.Now.AddDays(7);
cp2.Expires = DateTime.Now.AddDays(7);
loginDay = DateTime.Now.AddDays(7);
}
else
{
loginDay = DateTime.Now.AddMinutes(30);
}
MemecacheHelper.Set(sessionId, NewtonSoftHelper.ObjectToJsonStr(info), loginDay);
Response.Cookies.Add(cp1); Response.Cookies.Add(cp2);
script = DoLogin(info);
}
else
{
script = BootBoxDialog.Error();
}
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", script, true);
}private string DoLogin(UserInfo info)
{
UserInfo uinf = MongoDbHelper.FindOneByCondition<UserInfo>(MyenumHelper.DBChose.myfirstmog.ToString(), MyenumHelper.TableChose.person.ToString(), Query.And(Query<UserInfo>.EQ(c => c.name, info.name), Query<UserInfo>.EQ(c => c.pswd, info.pswd)));
if (uinf != null)
{
return BootBoxDialog.Succeed("登录成功 !");
}
else { return BootBoxDialog.Failure(); }
}
#endregion#region 注册的模块
protected void butregister_Click(object sender, EventArgs e)
{
string uname = Request.Params["uname"]; string upswd = Request.Params["upswd"];
string script = string.Empty;
if (uname.IsNotEmptOrNull() && upswd.IsNotEmptOrNull())
{
UserInfo info = new UserInfo();
info.name = uname; info.pswd = upswd;
if (MongoDbHelper.Insert<UserInfo>(MyenumHelper.DBChose.myfirstmog.ToString(), MyenumHelper.TableChose.person.ToString(),info))
{
script = BootBoxDialog.Succeed("恭喜,注册成功 !");
}
else
{
script = BootBoxDialog.Failure("注册失败!");
}
}
else
{
script = BootBoxDialog.Warning("账号密码必填项写 !");
}
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", script, true);
}
#endregion