原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token

为什么选择wcf?   因为好像wcf和wpf就是哥俩,,,

为什么选择异步调用?  用起来体验相对好一点,不会因为服务的速度影响用户体验,避免页面假死

首先新建一个wcf服务程序

public class ServiceLogin : IServiceLogin
{
public bool Login(string username, string pwd)
{
if ("root" == username && "root" == pwd)
{
return true;
}
else
{
return false;
}
}
public UserInfo GetUserInfo(string userid)
{
return new UserInfo() { username="root",fullname="管理员", role="admin",userid="1" };
}
}

这里暂时固定root用户,

客户端添加一个新的项目PW.SericeCenter,用来引用wcf服务,供其他各个模块调用

在引用时高级里勾选生成异步操作

ServiceComm 封装service,这里可以做一些数据的处理,比如PW.Common.UserInfo数据的转换,或者更为简便的你可以用json,因为wcf返回的对象Userinfo是在引用服务的生成的代码中,不宜做改动,最好是转换成自己的ViewModel,做数据绑定更为方便

public event System.EventHandler<ServicesEventArgs<bool>> LoginCompleted;
public void Login(string username,string pwd)
{
ServiceLogin.ServiceLoginClient client = new ServiceLogin.ServiceLoginClient();
client.LoginCompleted += (sender, e) =>
{
ServicesEventArgs<bool> arg = new ServicesEventArgs<bool>(); if (e.Error == null)
{
arg.Result = e.Result;
arg.Succesed = true;
}
else
{
arg.Succesed = false;
arg.Error = e.Error;
//写错误日志
//.....
}
if (LoginCompleted != null)
{
LoginCompleted.Invoke(this, arg);
}
};
client.LoginAsync(username, pwd);
} public event EventHandler<ServicesEventArgs<PW.Common.UserInfo>> GetUserInfoCompleted;
public void GetUserInfo(string userid)
{
ServiceLogin.ServiceLoginClient client = new ServiceLogin.ServiceLoginClient();
client.GetUserInfoCompleted += (sender, e) =>
{
ServicesEventArgs<PW.Common.UserInfo> arg = new ServicesEventArgs<PW.Common.UserInfo>(); if (e.Error == null)
{
if (e.Result != null)
{
arg.Result = new PW.Common.UserInfo() { fullname = e.Result.fullname, role = e.Result.role, userid = e.Result.userid, username = e.Result.username };
}
else
{
arg.Result = null;
} arg.Succesed = true;
}
else
{
arg.Succesed = false;
arg.Error = e.Error;
//写错误日志
//.....
}
if (GetUserInfoCompleted != null)
{
GetUserInfoCompleted.Invoke(this, arg);
}
};
client.GetUserInfoAsync(userid);
}

然后在login模块调用

ServiceComm sc = new ServiceComm();
sc.LoginCompleted += (serice, eve) =>
{
if (eve.Succesed && eve.Result)
{
//成功后balabala
GlobalData.EventAggregator.GetEvent<BaseDataLoadedEvent>().Publish(1);
LoadAllModules();
#region 独立存储区操作
try
{
if (IsolatedStorageFile.IsEnabled == true)
{
IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForAssembly();
IsolatedStorageFileStream isoFileStream = isoFile.OpenFile("login.txt", System.IO.FileMode.Create);
String loginStr = "";
if (this.cbxRemPassword.IsChecked == true)
{
loginStr = String.Format("{0}", this.txtName.Text.Trim());
}
Byte[] bts = Encoding.UTF8.GetBytes(loginStr);
isoFileStream.Write(bts, 0, bts.Length);
isoFileStream.Close();
isoFile.Dispose();
}
}
catch (Exception)
{ }
#endregion
GlobalData.UserName = this.txtName.Text.Trim();
}
else
{
//失败后balabala
this.btnLogin.IsEnabled = true;
this.gcLogin.Visibility = Visibility.Visible;
this.loadingInfo.Visibility = Visibility.Collapsed;
MessageBox.Show("登陆失败!");
}
};
sc.Login(this.txtName.Text.Trim(), this.txtPassword.Password.Trim());

写完这些吐槽MessageBox.Show("登陆失败!");真的不是一般的难看。。。。有时间换掉

就是这么不协调,,,,登录等待画面暂时有些粗糙,有时间美化一下

登录成功的就和之前一样了。。。

本事wpf去调用wcf服务很简单,但是我搞了一天,主要耗时在了安全验证上面了,

最初想用x509,看了写文章,自己也试了试,很是麻烦,安装证书比较坑,好像是有客户端免安装证书的,但我还是放弃了x509

最后看到了csdn这个问题

https://bbs.csdn.net/topics/390774814/

我就按照消息头拦截这个做的

感觉可以扩展,实现不同的客户端不同的token ,然后每个用户每次登陆带着不同的token,虽然token被人截取了还是不安全,,,再研究吧

具体怎么嵌入到我的项目的就不细说了,论坛有demo连接

 

 

Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token的更多相关文章

  1. Prism for WPF 搭建一个简单的模块化开发框架 (一个节点)

    原文:Prism for WPF 搭建一个简单的模块化开发框架 (一个节点) 这里我就只贴图不贴代码了,看看这个节点之前的效果 觉得做的好的地方可以范之前的文章看看 有好的建议也可以说说   填充数据 ...

  2. Prism for WPF 搭建一个简单的模块化开发框架(六)隐藏菜单、导航

    原文:Prism for WPF 搭建一个简单的模块化开发框架(六)隐藏菜单.导航 这个实际上是在聊天之前做的,一起写了,也不分先后了 看一下效果图,上面是模块主导航,左侧是模块内菜单,现在加一下隐藏 ...

  3. Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天、消息模块

    原文:Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天.消息模块 中秋节假期没事继续搞了搞 做了各聊天的模块,需要继续优化 第一步画页面 页面参考https://github.c ...

  4. Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单

    原文:Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单 昨天晚上把TreeView的样式做了一下,今天给TreeView绑了数据,实现了切换页面功能 上 ...

  5. Prism for WPF 搭建一个简单的模块化开发框架(二)

    原文:Prism for WPF 搭建一个简单的模块化开发框架(二) 今天又有时间了,再改改,加了一些控件全局的样式 样式代码 <ResourceDictionary xmlns="h ...

  6. Prism for WPF 搭建一个简单的模块化开发框架(一)

    原文:Prism for WPF 搭建一个简单的模块化开发框架(一) 最近闲来无事又想搞搞WPF..... 做个框架吧,可能又是半途而废....总是坚持不下来 不废话了, 先看一下工程结构 布局大概是 ...

  7. 用express搭建一个简单的博客系统

    转自:https://blog.csdn.net/qq_29721837/article/details/62055603 Express 简介 Express 是一个简洁而灵活的 node.js W ...

  8. 从零开始搭建一个简单的基于webpack的vue开发环境

    原文地址:https://segmentfault.com/a/1190000012789253?utm_source=tag-newest 从零开始搭建一个简单的基于webpack的react开发环 ...

  9. 用nodejs搭建一个简单的服务器

    使用nodejs搭建一个简单的服务器 nodejs优点:性能高(读写文件) 数据操作能力强 官网:www.nodejs.org 验证是否安装成功:cmd命令行中输入node -v 如果显示版本号表示安 ...

随机推荐

  1. February 9 2017 Week 6 Thursday

    Happy life lies in a peaceful mind. 幸福的生活存在于心绪的宁静当中. What can be seen as happy life? Maybe the answe ...

  2. 马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与 分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴CEO的马云自曝了他第一次上电视是在1995年。“我刚开始创

    马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与   分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴 ...

  3. LDA相关论文汇总

    转:http://blog.csdn.net/pirage/article/details/9467547 LDA理论 David M. Blei, Andrew Y. Ng, and Michael ...

  4. SAP CRM One Order跟踪和日志工具CRMD_TRACE_SET

    事务码CRMD_TRACE_SET激活跟踪模式: 在跟踪模式下运行One Order场景.运行完毕后,使用事务码CRMD_TRACE_EVAL: 双击参数,就能看到参数明细: 点Callstack也能 ...

  5. UI层实现

    领域驱动设计实践 —— UI层实现   目录 1. User Interface 2. Controller 3.  DTO 4.  infrastructure层的公共组件 5. UI层类图 6. ...

  6. Eclipse环境下配置Tomcat,并且把项目部署到Tomcat服务器上

    一 配置Tomcat 1.打开Eclipse,单击"Window"菜单,选择下方的"Preferences". 2.单击"Server"选项 ...

  7. 利用python 传输文件

    最近在学python3 发现了一个很有用的功能,该功能可以将安装python 的机器作为一台http 服务器来分享本机的文件, 具体的使用记录如下 python3 的使用方法 直接在windows 的 ...

  8. 解决Windows10下小娜无法搜索本地应用的问题

    适用场景 小娜突然出现各种问题.比如突然无法搜索到本地应用...等其它问题 一般使用下面的方法,将小娜进行重新注册就ok了. 解决方案 1.用管理员权限打开 C:\Windows\System32\W ...

  9. 快速排序_c++

    快速排序_c++ GitHub 文解 快速排序正如其名,是一种排序速度较快的排序算法. 其核心思想: 取数组的第一个数,确定其在整个数组中的位置. 以刚刚的数值所确定的位置经数组分为两个部分. 再分别 ...

  10. git 对文件大小写修改无反应 不敏感解决办法

    git config core.ignorecase false 执行之后就能自动检测到了 2019-01-18