需求:登录界面改成这样

记录一下过程,以便下次操作类似的步骤有遗忘,也与大伙儿分享下,如有不当之处请指出,感谢。

参考官网文档: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 框架自定义登录添加短信验证功能的更多相关文章

  1. Springboot下实现阿里云短信验证功能(含代码)

    Springboot下实现阿里云短信验证功能 一 开通阿里云短信服务 阿里云官网注册登录 找到短信服务并开通 打开短信服务的管理台 在国内消息那栏中添加签名管理和模板管理(按照格式要求去写) 在右上角 ...

  2. 如何实现php手机短信验证功能

    http://www.qdexun.cn/jsp/news/shownews.do?method=GetqtnewsdetailAction&id=1677 下载php源代码 现在网站在建设网 ...

  3. AndroidStudio短信验证功能收不到验证码

    http://mob.com/第三方接口获取地址: 登陆过后点我的后台即可上传,管理应用.需注意的是,即使验证不通过,只要整合了短信验证的Jar包,每天都有20条免费验证短信.现在的mob.com只支 ...

  4. 基于ThinkPHP与阿里大于的PHP短信验证功能

    https://blog.csdn.net/s371795639/article/details/53381274 PHP阿里大鱼短信验证 第一步 登陆阿里大于注册账号,在用户管理中心创建应用,确定A ...

  5. sendsms短信验证功能实现代码

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name ...

  6. Android Studio精彩案例(五)《JSMS短信验证码功能实现》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 很多应用刚打开的时候,让我们输入手机号,通过短信验证码来登录该应用.那么,这个场景是怎么实现的呢?其实是很多开放平台提供了短信验证功能 ...

  7. ASP.NET MVC+Bootstrap 实现短信验证

    短信验证大家都已经非常熟悉了,基本上每天都在接触手机短信的验证码,比方某宝,某东购物.站点注冊,网上银行等等,都要验证我们的手机号码真实性.这样做有什么优点呢. 曾经咱们在做站点的时候.为了提高用户注 ...

  8. 完整的Android手机短信验证源码

    短信验证功能我分两个模块来说,短信验证码的后台和代码实现短信验证码的功能. 一.短信验证码的后台      1.注册Mob账号:http://www.mob.com/#/login 2.注册成功之后, ...

  9. Android实战简易教程-第三十九枪(第三方短信验证平台Mob和验证码自己主动填入功能结合实例)

    用户注冊或者找回password时通常会用到短信验证功能.这里我们使用第三方的短信平台进行验证实例. 我们用到第三方短信验证平台是Mob,地址为:http://mob.com/ 一.注冊用户.获取SD ...

  10. JAVA短信验证登录

    短信验证登陆 1.点击触发,以电话号码为参数调用发送验证登录短信方法 2.默认模板为验证模板 生成6位验证码 3.将生成的验证码和手机号码放入缓存,(已经设置好缓存存放时间) 4.调用发送模板短信方法 ...

随机推荐

  1. 如何基于 Redis 实现分布式锁

    什么是分布式锁 分布式锁:不同进程必须以互斥方式使用共享资源的一种锁方法实现. 实现分布式锁的基础 互斥.任何时刻,只有一个客户端持有锁. 无死锁.最终总是有可能获得锁,即使持有锁的客户端已经崩溃. ...

  2. Flaks框架(Flask请求响应,session,闪现,请求扩展,中间件,蓝图)

    目录 一:Flask请求响应 1.请求相关信息 2.flask新手四件套 3.响应相关信息(响应response增加数据返回) 二:session 1.session与cookie简介 2.在使用se ...

  3. hyperf 配置 https 访问

    最近用hyperf写了支付系统,本地调试支付完成,打包上线部署,要解决https协议进行相应的访问,但是hyperf 官方没有找到相关的ssl配置说明.搜了一下soole还是有几个案例说明,据我的了解 ...

  4. 后疫情办公时代——你需要的多人同步协同编辑Demo(可粘贴可撤销)

    新冠病毒的疫情使得在线办公成为了一个常态,这使得在线文档成为了时下的热点.其中在线协同表格是在线文档的重要一个组成部分,纯前端表格在在线协同表格上有着得天独厚的优势:本身已经实现了单人操作在线文档的基 ...

  5. 【转载】EXCEL VBA UBound(arr,1),UBound(arr,2)解释

    Resize(UBound(arr, 1), UBound(arr, 2) 这句什么意思   resize()是一个扩展单元格地址区域的函数,有两个参数,第一个是行扩展数,第二个是列扩展数   UBo ...

  6. 对Asp.net WebApi中异步(async+await)接口实际使用及相关思考(示例给出了get,post,提交文件,异步接口等实践).

    [很多初学者的疑问] 为何作为web api这样的天然的并发应用,还需要在controller的action上声明使用async这些呢? <参考解答> 在 web 服务器上,.NET Fr ...

  7. 孤独的照片【USACO 2021 December Contest Bronze】

    孤独的照片 Farmer John 最近购入了 \(N\) 头新的奶牛,每头奶牛的品种是更赛牛(Guernsey)或荷斯坦牛(Holstein)之一. 奶牛目前排成一排,Farmer John 想要为 ...

  8. [LeetCode]最大连续1的个数

    题目 代码 class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { int length= ...

  9. P7076 [CSP-S2020] 动物园

    题面 动物园里饲养了很多动物,饲养员小 A 会根据饲养动物的情况,按照<饲养指南>购买不同种类的饲料,并将购买清单发给采购员小 B. 具体而言,动物世界里存在 \(2^k\) 种不同的动物 ...

  10. 从0开始学Java 第一期 开发前的准备

    Java 学习(一) - 开发前的准备 前言 由于一些项目上的需要,我得学习一下 Java 这门语言(主要是想写Android),本人并非0基础,至少在上个学期学习了一门必修的程序设计(C语言),所以 ...