Dev Express 框架自定义登录添加短信验证功能
需求:登录界面改成这样
记录一下过程,以便下次操作类似的步骤有遗忘,也与大伙儿分享下,如有不当之处请指出,感谢。
参考官网文档:https://docs.devexpress.com/eXpressAppFramework/112982/task-based-help/security/how-to-use-custom-logon-parameters-and-authentication?v=18.1
具体实现步骤:
一:界面
1.自定义用户类Employee(也可以使用默认默认的PermissionPolicyUser类,如果属性满足的话)
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.PermissionPolicy;
using DevExpress.Xpo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Login2.Module.BusinessObjects
{
[DefaultClassOptions, DefaultProperty("UserName")]
public class Employee : PermissionPolicyUser //继承默认的用户类
{
public Employee(Session session) : base(session) { }
//验证码
private string verificationCode; public string VerificationCode
{
get { return verificationCode; }
set { SetPropertyValue("VerificationCode", ref verificationCode, value); }
} //手机号
private string phone; public string Phone
{
get { return phone; }
set { SetPropertyValue("Phone", ref phone, value); }
}
}
}
2.自定义参数类:ConstomLogonParameters
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks; namespace Login.Module
{
[DomainComponent, Serializable]
[System.ComponentModel.DisplayName("Log In")]
public class CustomLogonParameters : INotifyPropertyChanged, ISerializable
{
private string username;
private string password;
private string verificationCode;
[Browsable(true)]
public String UserName
{
get { return username; }
set
{
if (username == value) return;
username = value;
}
}
[PasswordPropertyText(true)]
public string Password
{
get { return password; }
set
{
if (password == value) return;
password = value;
}
}
//验证码
[Browsable(true)]
public String VerificationCode
{
get { return verificationCode; }
set
{
if (verificationCode == value) return;
verificationCode = value;
}
} public CustomLogonParameters() { }
// ISerializable
public CustomLogonParameters(SerializationInfo info, StreamingContext context)
{
if (info.MemberCount > 0)
{
UserName = info.GetString("UserName");
Password = info.GetString("Password");
VerificationCode = info.GetString("VerificationCode");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged; [System.Security.SecurityCritical]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("UserName", UserName);
info.AddValue("Password", Password);
info.AddValue("VerificationCode", VerificationCode);
}
}
}
3.自定义参数验证类:CustomAuthentication 类
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security;
using DevExpress.Persistent.BaseImpl.PermissionPolicy;
using Login.Module.BusinessObjects;
using Login2.Module;
using Login2.Module.BusinessObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Login.Module
{
public class CustomAuthentication : AuthenticationBase, IAuthenticationStandard
{
private CustomLogonParameters customLogonParameters;
public CustomAuthentication()
{
customLogonParameters = new CustomLogonParameters();
}
public override void Logoff()
{
base.Logoff();
customLogonParameters = new CustomLogonParameters();
}
public override void ClearSecuredLogonParameters()
{
customLogonParameters.Password = "";
base.ClearSecuredLogonParameters();
} // 自定义的用户类 public override object Authenticate(IObjectSpace objectSpace)
{
//根据用户名查找该用户
Employee employee = objectSpace.FindObject<Employee>
(new BinaryOperator("UserName", customLogonParameters.UserName));
//用户不存在
if (employee == null)
throw new ArgumentNullException("Employee");
//比较密码
if (!employee.ComparePassword(customLogonParameters.Password))
throw new AuthenticationException(
employee.UserName, "Password mismatch.");
//比较验证码
string verificationCode = employee.VerificationCode;
if (verificationCode != customLogonParameters.VerificationCode)
throw new AuthenticationException("VerificationCode mismatch"); return employee; } public override void SetLogonParameters(object logonParameters)
{
this.customLogonParameters = (CustomLogonParameters)logonParameters;
} public override IList<Type> GetBusinessClasses()
{
return new Type[] { typeof(CustomLogonParameters) };
}
public override bool AskLogonParametersViaUI
{
get { return true; }
}
public override object LogonParameters
{
get { return customLogonParameters; }
}
public override bool IsLogoffEnabled
{
get { return true; }
}
}
}
4.WinApplications.cs 中替换组件

替换这个登录界面上的参数才会是我们自定义的参数,如下:

5.添加“发送验证码”按钮:
1)在Controller包下 Add Dev Express Item 选择view Controller

2)给视图层添加按钮组件:

3)设置组件属性
设置属性时,项目不能处于启动状态,不然设置不上去

属性中的TargetViewId,填下面的这个自定义参数类的detail_View 的 Id

接下来,我调了半天.....梳理一下
新建如下,
在layout下 再把上面建的组件添加上去,具体细节....

右键 add layoutViewItem


接下来就是 Controller包中添加的那个SimpleAction的属性了。

之后,再把新增的这个组件给注册了。在Module.cs 文件中添加代码。
参考:https://docs.devexpress.com/eXpressAppFramework/113475/ui-construction/controllers-and-actions/logon-form-controllers-and-actions
这个注册是组长在论坛上搜索到的。
之后界面就调好了。

二:验证
验证就稍微简单一点了。
根据它官网给的实例代码,找到校验的方法。

再根据需求自定义就好了。
还有就是发送短信验证码的按钮执行方法,在按钮的属性中可以找到。或者直接点击这个ViewController。再方法中自定义方法,例如:
private void simpleAction_yzm_Execute(object sender, SimpleActionExecuteEventArgs e)
{ customLogonParameters = e.CurrentObject as CustomLogonParameters;
string username = customLogonParameters.UserName;
if(username == null || username == "")
{
throw new AuthenticationException("请输入用户名"); }
IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(PermissionPolicyUser));
Employee user = objectSpace.FindObject<Employee>
(new BinaryOperator("UserName", customLogonParameters.UserName)); string phone = user.Phone; //2.向该手机发送验证码
string verificationCode = "1234";
//验证码赋值给 用户的验证码属性 比较的时候 用 用户的验证码属性和输入的校验
//employee.VerificationCode = verificationCode; user.VerificationCode = verificationCode;
objectSpace.CommitChanges(); }
注意这里按钮取登录页面上输入的参数用的对象和方法与点击Log In 有略微不同。
附CustomLogonParameter代码如下:
public class CustomLogonParameters : INotifyPropertyChanged, ISerializable
{
private string username;
private string password;
private string verificationCode; [Browsable(true)]
public String UserName
{
get { return username; }
set
{
if (username == value) return;
username = value;
}
}
[PasswordPropertyText(true)]
public string Password
{
get { return password; }
set
{
if (password == value) return;
password = value;
}
}
//验证码
[Browsable(true)]
public String VerificationCode
{
get { return verificationCode; }
set
{
if (verificationCode == value) return;
verificationCode = value;
}
} public CustomLogonParameters() { }
// ISerializable
public CustomLogonParameters(SerializationInfo info, StreamingContext context)
{
if (info.MemberCount > 0)
{
UserName = info.GetString("UserName");
Password = info.GetString("Password");
VerificationCode = info.GetString("VerificationCode");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged; [System.Security.SecurityCritical]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("UserName", UserName);
info.AddValue("Password", Password);
info.AddValue("VerificationCode", VerificationCode);
}
CustomAuthentication类代码如下:
public class CustomAuthentication : AuthenticationBase, IAuthenticationStandard
{
private CustomLogonParameters customLogonParameters;
public CustomAuthentication()
{
customLogonParameters = new CustomLogonParameters();
}
public override void Logoff()
{
base.Logoff();
customLogonParameters = new CustomLogonParameters();
}
public override void ClearSecuredLogonParameters()
{
customLogonParameters.Password = "";
base.ClearSecuredLogonParameters();
}
//public override object Authenticate(IObjectSpace objectSpace)
//{ // Employee employee = objectSpace.FindObject<Employee>(
// new BinaryOperator("UserName", customLogonParameters.UserName)); // if (employee == null)
// throw new ArgumentNullException("Employee"); // if (!employee.ComparePassword(customLogonParameters.Password))
// throw new AuthenticationException(
// employee.UserName, "Password mismatch."); // return employee;
//} // 默认的用户类
//public override object Authenticate(IObjectSpace objectSpace)
//{
// //根据用户名查找该用户
// PermissionPolicyUser sampleUser = objectSpace.FindObject<PermissionPolicyUser>
// (new BinaryOperator("UserName", customLogonParameters.UserName));
// //用户不存在
// if (sampleUser == null)
// throw new ArgumentNullException("PermissionPolicyUser");
// //比较密码
// if (!sampleUser.ComparePassword(customLogonParameters.Password))
// throw new AuthenticationException(
// sampleUser.UserName, "Password mismatch.");
// //比较验证码
// //if(sampleUser.VerificationCode!=customLogonParameters.VerificationCode)
// if ("1234" != customLogonParameters.VerificationCode)
// throw new AuthenticationException("VerificationCode mismatch"); // return sampleUser; //} // 自定义的用户类 public override object Authenticate(IObjectSpace objectSpace)
{
//根据用户名查找该用户
Employee employee = objectSpace.FindObject<Employee>
(new BinaryOperator("UserName", customLogonParameters.UserName));
//用户不存在
if (employee == null)
throw new ArgumentNullException("Employee");
//比较密码
if (!employee.ComparePassword(customLogonParameters.Password))
throw new AuthenticationException(
employee.UserName, "Password mismatch.");
//比较验证码
string verificationCode = employee.VerificationCode;
if (verificationCode != customLogonParameters.VerificationCode)
throw new AuthenticationException("VerificationCode mismatch"); return employee; } public override void SetLogonParameters(object logonParameters)
{
this.customLogonParameters = (CustomLogonParameters)logonParameters;
} public override IList<Type> GetBusinessClasses()
{
return new Type[] { typeof(CustomLogonParameters) };
}
public override bool AskLogonParametersViaUI
{
get { return true; }
}
public override object LogonParameters
{
get { return customLogonParameters; }
}
public override bool IsLogoffEnabled
{
get { return true; }
}
写的比较匆忙,如有不当欢迎指出!
Dev Express 框架自定义登录添加短信验证功能的更多相关文章
- Springboot下实现阿里云短信验证功能(含代码)
Springboot下实现阿里云短信验证功能 一 开通阿里云短信服务 阿里云官网注册登录 找到短信服务并开通 打开短信服务的管理台 在国内消息那栏中添加签名管理和模板管理(按照格式要求去写) 在右上角 ...
- 如何实现php手机短信验证功能
http://www.qdexun.cn/jsp/news/shownews.do?method=GetqtnewsdetailAction&id=1677 下载php源代码 现在网站在建设网 ...
- AndroidStudio短信验证功能收不到验证码
http://mob.com/第三方接口获取地址: 登陆过后点我的后台即可上传,管理应用.需注意的是,即使验证不通过,只要整合了短信验证的Jar包,每天都有20条免费验证短信.现在的mob.com只支 ...
- 基于ThinkPHP与阿里大于的PHP短信验证功能
https://blog.csdn.net/s371795639/article/details/53381274 PHP阿里大鱼短信验证 第一步 登陆阿里大于注册账号,在用户管理中心创建应用,确定A ...
- sendsms短信验证功能实现代码
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name ...
- Android Studio精彩案例(五)《JSMS短信验证码功能实现》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 很多应用刚打开的时候,让我们输入手机号,通过短信验证码来登录该应用.那么,这个场景是怎么实现的呢?其实是很多开放平台提供了短信验证功能 ...
- ASP.NET MVC+Bootstrap 实现短信验证
短信验证大家都已经非常熟悉了,基本上每天都在接触手机短信的验证码,比方某宝,某东购物.站点注冊,网上银行等等,都要验证我们的手机号码真实性.这样做有什么优点呢. 曾经咱们在做站点的时候.为了提高用户注 ...
- 完整的Android手机短信验证源码
短信验证功能我分两个模块来说,短信验证码的后台和代码实现短信验证码的功能. 一.短信验证码的后台 1.注册Mob账号:http://www.mob.com/#/login 2.注册成功之后, ...
- Android实战简易教程-第三十九枪(第三方短信验证平台Mob和验证码自己主动填入功能结合实例)
用户注冊或者找回password时通常会用到短信验证功能.这里我们使用第三方的短信平台进行验证实例. 我们用到第三方短信验证平台是Mob,地址为:http://mob.com/ 一.注冊用户.获取SD ...
- JAVA短信验证登录
短信验证登陆 1.点击触发,以电话号码为参数调用发送验证登录短信方法 2.默认模板为验证模板 生成6位验证码 3.将生成的验证码和手机号码放入缓存,(已经设置好缓存存放时间) 4.调用发送模板短信方法 ...
随机推荐
- MySQL事务(四大特征)-存储过程
目录 一:事务 1.四大特性(ACID) 2.事物存在的必要性(真实比喻) 3.如何使用事务 4.开启事务-回滚-确认 二:事务案例实战 1.模拟消费 2.创建 3.插入数据 4.开启事务 5.修改操 ...
- C/S UDP通信实践踩坑记录与对于ICMP的进一步认识
背景 最近有个业务场景需要服务端(简称S)与客户端(简称C)设计一套基于UDP的通信协议--要求尽可能快的前提下可容忍一定丢包率,得以比较深入地学习和了解UDP通信和实践,在开发调试期间先后碰到了C端 ...
- C#开发的磁吸屏幕类库 - 开源研究系列文章
上次写了一个关于线程池的博文,里面讲到了关于磁吸屏幕的类库,今天就把这个类库进行下讲解. 一. 类库目录: 类库的目录见下图,主要定义了Win32的一些API,以及一些API使用到的常量和结 ...
- ubunut安装qtcreater
安装gcc 1 kxb@kxb:~$ gcc -v 2 3 Command 'gcc' not found, but can be installed with: 4 5 sudo apt insta ...
- 说一下三种jar包在Linux的启动方式
最近在linux上发布了一个SpringBoot项目,研究了一下jar包在Linux上的集中启动方式,特在此分享一下. 这里默认要启动的jar包为demo-0.0.1-SNAPSHOT.jar 第一种 ...
- Java学习笔记:2022年1月10日
Java学习笔记:2022年1月10日 摘要:这篇笔记主要记录了学习<Java核心技术 卷一>的第四章时的一些心得,主要阐述了对象与类这一部分的内容.需要注意的是,这一章的内容需要精心 ...
- Entrypoint undefined = index.html html-webpack-plugin 错误ERROR in Error: Child compilation failed: Module build failed (from ./node_modules/html-webpack-plu SyntaxError: Unexpected token )
Entrypoint undefined = index.html html-webpack-plugin 错误 ERROR in Error: Child compilation failed: M ...
- Grafana 系列文章(三):Tempo-使用 HTTP 推送 Spans
️URL: https://grafana.com/docs/tempo/latest/api_docs/pushing-spans-with-http/ Description: 有时,使用追踪系统 ...
- java入门与进阶 P-2.7+P-3.1
判断语句常见错误 忘了大括号 永远在if和else后面加上大括号,即使当时后面只有一条语句 if后面的分号 错误使用== 和= if只要求()里面的值是零或者非零 = 是赋值 == 是判断是否相同 代 ...
- Java 进阶P-4.6+P-4.7
向上造型 造型cast 子类的对象可以赋值给父类的变量 注意! java中不存在对象对对象的赋值!! 父类的对象不能赋值给子类的变量 可以用造型: c=(Car) v; (只有当v这个变量实际管理的是 ...