使用Silverlight构建应用程序(五)
用户的注册和登录:
前边说过主程序框架除了对插件提供宿主之外,另外主要就是提供用户管理功能。这次做用户的注册和登录功能:
整个用户的操作的WCF服务都在WSMain.SVC服务中,主要提供了如下地方法:
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WSMain
{
[OperationContract]
public bool Login(string userName,string passWord)
{
MainBLL bll = new MainBLL();
return bll.Login(userName,passWord);
}
[OperationContract]
public string Regiest(UserInfo userInfo)
{
MainBLL bll = new MainBLL();
return bll.Regiest(userInfo);
}
[OperationContract]
public UserInfo GetUserInfo(string userName)
{
UserInfo result = new UserInfo();
return result;
}
[OperationContract]
public List<UserRelaInfo> GetFriends(string userName)
{
List<UserRelaInfo> result = new List<UserRelaInfo>();
MainBLL bll = new MainBLL();
result = bll.GetFriends(userName);
return result;
}
[OperationContract]
public bool UpDateLocalStyle(string userRelationId, string localStyle)
{
bool result = true;
MainBLL bll = new MainBLL();
bll.UpDateLocalStyle(userRelationId, localStyle);
return result;
}
// Add more operations here and mark them with [OperationContract]
}。
具体的实现界面请参照牛人的一篇文章:http://www.cnblogs.com/RChen/archive/2008/07/05/1236046.html
这是服务器端的服务,我们看看客户端的调用:
private void UserRegion(string userName, string userMail, string passWord)
{
WSMain.UserInfo userInfo = new WSMain.UserInfo();
passWord = MD5CryptoServiceProvider.GetMd5String(passWord);
passWord = MD5CryptoServiceProvider.GetMd5String(userName + passWord);
//
userInfo.UserName = userName;
userInfo.PassWord = passWord;
userInfo.EMail = userMail;
//
Uri uri = System.Windows.Browser.HtmlPage.Document.DocumentUri;
string host = uri.AbsoluteUri;
host = host.Substring(0, host.Length - uri.LocalPath.Length);
string servicePath = "/Services/WSMain.svc";
string serviceUri = host + servicePath;
WSMainClient wsMain = new WSMainClient(new System.ServiceModel.BasicHttpBinding(), new System.ServiceModel.EndpointAddress(serviceUri));
wsMain.RegiestCompleted += (o, ev) =>
{
if (ev.Error == null)
{
string result = ev.Result;
if (result == "True")
{
//设定用户信息
PublicUnit.UserInfo.UserName = userName;
PublicUnit.UserInfo.UsereMail = userMail;
PublicUnit.UserInfo.IsLogined = true;
//关闭当前窗口
Close();
}
else
{
this.txtMesage.Text = result;
}
}
};
wsMain.RegiestAsync(userInfo);
}
}
这是客户端的调用方法,其中用户密码使用md5进行离散,然后再和用户名相加进行第二次离散。这样出来的结果基本不可能被现有的md5查询网站查询出来密码。
然后调用服务器端的注册方法,本来可以返回bool类型的,但是有点偷懒,就返回了一个字符串,当字符串是”True”的时候表示注册成功,其他表示失败,返回的是失败的原因。
注册成功之后,就设定一个静态变量。这个静态变量就是整个宿主程序的登陆用户。
然后关闭窗口,至此,用户注册完成。其实大家可以看到,当用户注册成功之后,就进入了自动登陆状态。整个主界面上的输入用户密码的登陆就隐藏了,换成了查看用户信息。这个是使用用户的登陆事件:
public class PublicUnit
{
public static string PluginConfigFile = "WindCloud.Config.xml";
public static MainUser UserInfo = new MainUser();
}
public class MainUser : UserInfo
{
public delegate void Longined(Object sender, bool isLogined);
public event Longined OnLongined;
private bool logined = false;
public bool IsLogined
{
get
{
return logined;
}
set
{
logined = value;
if (OnLongined != null)
{
OnLongined(this, logined);
}
}
}
}
同时在主框架进入的时候,就加了一个事件:
PublicUnit.UserInfo.OnLongined += (o, ev) =>
{
if (ev)//用户成功登录
{
this.UserControl.LableUserName.Text = PublicUnit.UserInfo.UserName;
this.UserControl.TitleCanvasLogin.Visibility = Visibility.Collapsed;
this.UserControl.TitleCanvasLogOut.Visibility = Visibility.Visible;
//用户登录成功,通知所有插件用户发生改变
foreach (KeyValuePair<string, IPlugIn> kvp in pluginServices)
{
kvp.Value.ChangeUser(PublicUnit.UserInfo);
}
}
else//用户登出或者未登录
{
this.UserControl.LableUserName.Text = "";
this.UserControl.TitleCanvasLogin.Visibility = Visibility.Visible;
this.UserControl.TitleCanvasLogOut.Visibility = Visibility.Collapsed;
}
};
只需要改变用户是否登录属性就可以控制主框架上登陆的状态显示。
//至此,用户注册完成,下面看看用户登录:
//计算生成密码,密码生成规则:
//1:对密码使用MD5离散;
//2:把用户和离散的密码相加;
//3:把结果再MD5离散。
//这样的结果基本上现在的查询密码的网站不会查询出来,保证安全
//而且密码是在客户端计算的,网络传输也不会有问题
string userName = this.txtUserName.Text;
string passWord = this.txtPassWord.Password;
passWord = MD5CryptoServiceProvider.GetMd5String(passWord);
passWord = MD5CryptoServiceProvider.GetMd5String(userName + passWord);
//
Uri uri = System.Windows.Browser.HtmlPage.Document.DocumentUri;
string host = uri.AbsoluteUri;
host = host.Substring(0, host.Length - uri.LocalPath.Length);
string servicePath = "/Services/WSMain.svc";
string serviceUri = host + servicePath;
WSMainClient wsMain = new WSMainClient(new System.ServiceModel.BasicHttpBinding(), new System.ServiceModel.EndpointAddress(serviceUri));
wsMain.LoginCompleted += (o, ev) =>
{
if (ev.Error == null)
{
//登录成功
if (ev.Result)
{
//设定用户信息
PublicUnit.UserInfo.UserName = userName;
//PublicUnit.UserInfo.UsereMail = userMail;
PublicUnit.UserInfo.IsLogined = true;
}
else
{
//登录失败
MessageBox.Show("用户不存在或密码错误!请重新输入。"n如果是未注册用户,请先注册。", "提示", MessageBoxButton.OK);
}
}
};
wsMain.LoginAsync(userName, passWord);
这就是整个用户的登陆,也是使用设定用户的登陆状态是否存在。
至此,整个用户注册和登录完成。
欢迎对开源项目有兴趣的朋友加入这个项目组,招收开源开发人员。
预览:www.cuface.cn (SilverLight RC0)