需求:登录界面改成这样

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

参考官网文档: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. 想早点下班?试试Aorm库吧,更方便的进行Go数据库操作

    使用go进行项目开发,大多数人会使用gorm,但是gorm有一些缺点,我无法接受.于是开发出了aorm,目前能有满足日常开发需求,并且完善了使用文档,希望能够帮助到大家. Aorm Golang操作数 ...

  2. [py]残留python.exe导致anaconda python路径无法识别

    刚才重下anaconda真是给我整没脾气了 路径啥的都加好了,cmd输入python还是没有,给我跳应用商店去了- 重启也没用 经过一番搜索,找到解决办法: cmd输入"where pyth ...

  3. 什么是Auth模块?(全面了解)

    目录 一:Auth模块 1.什么是Auth模块? 2.Auth模块作用 二:引入Auth模块 1.其实我们在创建好一个Django项目之后直接执行数据库迁移命令会自动生成很多表 2.django在启动 ...

  4. MongoDB 强制使用索引 hint

    转载请注明出处: 虽然MongoDB 查询优化器一般工作的很不错,但是也可以使用 hint() 来强迫 MongoDB 使用一个特定的索引.在这种方法下某些情形下会提升性能. 一个有索引的 colle ...

  5. 【机器学习】李宏毅——Adversarial Attack(对抗攻击)

    研究这个方向的动机,是因为在将神经网络模型应用于实际场景时,它仅仅拥有较高的正确率是不够的,例如在异常检测中.垃圾邮件分类等等场景,那些负类样本也会想尽办法来"欺骗"模型,使模型无 ...

  6. Python从入门到精通(第2版)——pyuic5: error: no such option: -m的问题解决

    前言 在学习<Python从入门到精通(第2版)>的第15章 GUI界面编程--15.2.4 将.ui文件转换为.py文件时,按照书中步骤出错时的问题解决,希望对同样学习本书的同学有所帮助 ...

  7. VsCode搭建C语言运行环境以及终端乱码问题解决

    在VsCode中搭建C/C++运行环境需要先安装以下插件 1.安装c/c++插件 2.安装code runner插件 当然也可以安装一些其他的美化插件根据个人习惯,但是以上这两个是必装的. 安装好插件 ...

  8. solidity 内存(memory) 可变数组的增删改查 操作

    // SPDX-License-Identifier: MIT pragma solidity ^0.8.9; library Array { function push(uint256[] memo ...

  9. 题解 CF1579G Minimal Coverage

    CF1579G Minimal Coverage dp好题! link to the problem 解法 首先需要观察到:如果最长线段的长度为\(maxL\),那么答案不可能超过\(2maxL\) ...

  10. __int128:懒人的福音

    前言 对于一个懒懒的,不想写高精的人(就是我),每次都会遭遇到答案爆$long$  $long$的危险 比如说这道题: 题目传送门 最后的$23-25$的两个点,$long$  $long$甚至$un ...