原文: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. 如何在SAP里创建configurable material物料主数据

    (1) 使用tcode CT04创建characteristic: assign 所有可能的color value: (2) 使用tcode CL02创建class. 类型选择300- variant ...

  2. UML用例图间关系说明

    用例间一般存在如下四种关系: 1."通信"关系(<<cmmunicate>>构造型): "通信"关系:使用实心的关联线或带<< ...

  3. c++内存区域结构及堆栈的一些知识

    一.c++在内存区域的分配图 可以看出,对于Linux系统下的,存储空间的分配有着较为层次清晰的分层.单片机大概也遵循这个分区架构. 二进制代码以及常量(CONST修饰)以及全局变量在最底层,存储空间 ...

  4. 跨域问题时的Filter无效

    我页面用Web Uploader进行图片上传,后台使用一个过滤器解决跨域的options问题,然后我给入口类加上了这个过滤器注解配置,但是无效页面代码: <body> <div id ...

  5. Windows pycharm Terminal使用Anaconda 的Prompt

    从Stack Overflow上找到的方法如下 在Settings->Terminal->Shell path 改成:cmd.exe "/K" "C:\Use ...

  6. Selenium应用代码(登录)

    这篇可以不看,主要是为了以后的应用代码(传参)做铺垫. import java.awt.Rectangle; import java.awt.image.BufferedImage;import ja ...

  7. [转]Python中下划线以及命名空间的意义

    Python 用下划线作为变量前缀和后缀指定特殊变量/方法. 主要存在四种情形 1. 1. object # public    2. __object__ # special, python sys ...

  8. 智能指针 与 oc中的指针

     智能指针 与 oc中的指针 智能指针的原理及实现 当类中有指针成员时,一般有两种方式来管理指针成员:一是采用值型的方式管理,每个类对象都保留一份指针指向的对象的拷贝:另一种更优雅的方式是使用智能指针 ...

  9. 纯 js 让浏览器不缓存 ajax 请求

    开发「bufpay.com 个人即时到账收款平台」支付页面需要用到 ajax 轮询订单的支付状态. 现在浏览器对 ajax 的缓存策略遵循 http response header 里面的缓存设置,为 ...

  10. 『ACM C++』 PTA 天梯赛练习集L1 | 048-49

    今日刷题048-049 ------------------------------------------------L1-048---------------------------------- ...