public class UserRepository1.docx
- 文档编号:29903523
- 上传时间:2023-08-03
- 格式:DOCX
- 页数:23
- 大小:161.69KB
public class UserRepository1.docx
《public class UserRepository1.docx》由会员分享,可在线阅读,更多相关《public class UserRepository1.docx(23页珍藏版)》请在冰豆网上搜索。
publicclassUserRepository1
publicclassUserRepository
{
///
///addnewregister
///
///
///
///
publicboolAddNewRegisterUser(db_UserInfouser,refstringmsg)
{
try
{
using(GamdDataDbmlDataContextcontext=newGamdDataDbmlDataContext())
{
user.UserPwd=Encrypt(user.UserPwd,true);
context.db_UserInfos.InsertOnSubmit(user);
context.SubmitChanges();
}
returntrue;
}
catch(Exceptionex)
{
msg=ex.Message;
returnfalse;
}
}
///
///validatetheuser'susernameandpassword
///
///
///
///
///
///
///
publicboolValidateUserLogin(Stringaccount,stringpassword,refstringuseremail,refstringuserrole,refstringmsg)
{
if(String.IsNullOrEmpty(account)||String.IsNullOrEmpty(password))
{
msg="Parameterisinvalid.";
returnfalse;
}
try
{
using(GamdDataDbmlDataContextcontext=newGamdDataDbmlDataContext())
{
password=Encrypt(password,true);
varwusers=fromuincontext.db_UserInfos
whereu.UserName==account&&u.UserPwd==password
selectu;
if(wusers.Count()>0)
{
useremail=wusers.FirstOrDefault().UserEmail;
userrole=wusers.FirstOrDefault().UserRole;
returntrue;
}
returnfalse;
}
}
catch(Exceptionex)
{
msg=ex.Message;
returnfalse;
}
}
///
///getalltheusers
///
///
publicList
{
returnnewGamdDataDbmlDataContext().db_UserInfos.ToList();
}
publicvoidSetUserAsAdmin(stringuserName)
{
try
{
using(GamdDataDbmlDataContextdb=newGamdDataDbmlDataContext())
{
varus=fromuindb.db_UserInfos
whereu.UserName==userName
selectu;
if(us.FirstOrDefault()!
=null)
{
us.FirstOrDefault().UserRole="admin";
db.SubmitChanges();
}
}
}
catch(Exceptione)
{
}
}
///
///theuser'spasswordissavedinthedatabase,andthepasswordshouldbeencryption
///
#region====================Encryption====================
privatestaticstringkey="Gameisinteresting";
///
///encryption
///
///
///
///
publicstaticstringEncrypt(stringtoEncrypt,booluseHashing)
{
byte[]keyArray;
byte[]toEncryptArray=UTF8Encoding.UTF8.GetBytes(toEncrypt);
if(useHashing)
{
MD5CryptoServiceProviderhashmd5=newMD5CryptoServiceProvider();
keyArray=hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray=UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvidertdes=newTripleDESCryptoServiceProvider();
tdes.Key=keyArray;
tdes.Mode=CipherMode.ECB;
tdes.Padding=PaddingMode.PKCS7;
ICryptoTransformcTransform=tdes.CreateEncryptor();
byte[]resultArray=cTransform.TransformFinalBlock(toEncryptArray,0,toEncryptArray.Length);
tdes.Clear();
returnConvert.ToBase64String(resultArray,0,resultArray.Length);
}
///
///decryption
///
///
///
///
publicstaticstringDecrypt(stringcipherString,booluseHashing)
{
byte[]keyArray;
byte[]toEncryptArray=Convert.FromBase64String(cipherString);
if(useHashing)
{
MD5CryptoServiceProviderhashmd5=newMD5CryptoServiceProvider();
keyArray=hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray=UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvidertdes=newTripleDESCryptoServiceProvider();
tdes.Key=keyArray;
tdes.Mode=CipherMode.ECB;
tdes.Padding=PaddingMode.PKCS7;
ICryptoTransformcTransform=tdes.CreateDecryptor();
byte[]resultArray=cTransform.TransformFinalBlock(toEncryptArray,0,toEncryptArray.Length);
tdes.Clear();
returnUTF8Encoding.UTF8.GetString(resultArray);
}
#endregion
}
publicclassGameRepository
{
///
///addnewgame
///
///
///
///
publicboolAddNewGame(db_GameInfogame,refstringmsg)
{
try
{
using(GamdDataDbmlDataContextcontext=newGamdDataDbmlDataContext())
{
context.db_GameInfos.InsertOnSubmit(game);
context.SubmitChanges();
}
returntrue;
}
catch(Exceptionex)
{
msg=ex.Message;
returnfalse;
}
}
///
///getallgames
///
///
publicList
{
try
{
returnnewGamdDataDbmlDataContext().db_GameInfos.ToList();
}
catch(Exceptionex)
{
returnnull;
}
}
///
///searchthegamesofauser
///
///
///
publicList
{
try
{
using(GamdDataDbmlDataContextcontext=newGamdDataDbmlDataContext())
{
vargames=fromuincontext.db_GameInfos
whereu.GameCreater==gameCreater
selectu;
returngames.ToList();
}
}
catch(Exceptione)
{
returnnull;
}
}
///
///updategameinformation
///
///
publicvoidUpdateGame(stringgamename,stringcor,doublesize,DateTimetime)
{
using(GamdDataDbmlDataContextcontext=newGamdDataDbmlDataContext())
{
vargames=fromuincontext.db_GameInfos
whereu.GameName==gamename
selectu;
if(games.FirstOrDefault()!
=null)
{
games.FirstOrDefault().GameCor=cor;
games.FirstOrDefault().GameSize=size;
games.FirstOrDefault().GameTime=time;
context.SubmitChanges();
}
}
}
///
///deleteagame
///
///
publicvoidDeleteGame(stringgamename)
{
using(GamdDataDbmlDataContextcontext=newGamdDataDbmlDataContext())
{
vargames=fromuincontext.db_GameInfos
whereu.GameName==gamename
selectu;
if(games.FirstOrDefault()!
=null)
{
context.db_GameInfos.DeleteOnSubmit(games.FirstOrDefault());
context.SubmitChanges();
}
}
}
///
///searchgamewhichisaccordancewithsomecondition
///
///
///
///
///
///
///
///
///
publicList
{
try
{
using(GamdDataDbmlDataContextcontext=newGamdDataDbmlDataContext())
{
vargames=fromuincontext.db_GameInfos
whereu.GameName.Contains(gamename)&&u.GameSize>gamesize&&u.GameSize selectu; returngames.ToList(); } } catch(Exceptione) { returnnull; } } } //loginasacommonuseroradmin protectedvoidLoginButton_Click(objectsender,EventArgse) { stringname=UserName.Text; stringpwd=Password.Text; UserRepositoryur=newUserRepository(); stringemail=""; stringrole=""; stringmsg=""; if(ur.ValidateUserLogin(name,pwd,refemail,refrole,refmsg)) { Session["CurrentUserName"]=name; Session["CurrentUserEmail"]=email; Session["CurrentUserRole"]=role; Response.Redirect("~/Default.aspx"); } else { } } //loginthesystemasaguest protectedvoidGuestButton_Click(objectsender,EventArgse) { Session["CurrentUserRole"]="guest"; Response.Redirect("~/Default.aspx"); } //logout protectedvoidPage_Load(objectsender,EventArgse) { Session.Clear(); Session.RemoveAll(); Session.Abandon(); Response.Redirect("~/Default.aspx"); }
<%
if(Session["CurrentUserRole"].ToString().ToLower().Equals("admin"))
{
%>
transparent">AddNewGame
transparent">GameManagement
transparent">UserManagement
transparent">Logout
<%}
if(Session["CurrentUserRole"].ToString().ToLower().Equals("user"))
{
%>
transparent">AddNewGame
transparent">GameManagement
transparent">Logout
<%}
if(Session["CurrentUserRole"].ToString().ToLower().Equals("guest"))
{
%>
transparent">Login
<%}%>
transparent">HomePage
//createauser
protectedvoidCreateUserButton_Click(objectsender,EventArgse)
{
db_UserInfouserTemp=newdb_UserInfo();
userTemp.UserName=UserName.Text;
userTemp.UserPwd=Password.Text;
userTemp.UserRole="user";
userTemp.UserEmail=Email.Text;
stringmsg="";
UserRepositoryur=newUserRepository();
if(ur.AddNewRegisterUser(userTemp,refmsg))
{
Response.Write("
//Session["CurrentUserName"]=UserName.Text;
//Session[
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- public class UserRepository1