1、登录功能实现要点
对于登录系统,应该注意几个要点:用户认证流程设计,密码存储与验证,会话管理,防暴力破解措施,错误处理与提示
2、登录功能的视图模型
首先在xaml文件中必须指定该页面使用的视图模型,即viewmodel,ViewModel是WPF中MVVM模式的核心,负责管理数据和交互逻辑。通过实现INotifyPropertyChanged和ICommand接口,可以轻松实现数据绑定和命令绑定。使用MVVM框架可以进一步简化开发流程。ViewModel的主要职责是封装业务逻辑和数据状态,为View提供数据绑定和命令绑定的接口。它不直接依赖View,而是通过数据绑定机制与View交互,从而实现松耦合。
定义ViewModel类 ViewModel通常是一个普通的C#类,需要实现INotifyPropertyChanged接口以支持数据绑定通知,ViewModel还可以处理用户交互逻辑,通常通过ICommand接口实现
完整代码如下:
using HQ.BLL;
using HQ.COMM;
using HQ.fResApp.BaseModel;
using HQ.fResApp.Utils;
using HQ.MODEL.UIModel;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace HQ.fResApp.ViewModel
{
public class LoginVModel:ViewModelBase
{
UserBLL userBLL=new UserBLL();
LoginUser userInfo;
public LoginVModel()
{
userInfo = new LoginUser();
}
/// <summary>
/// 帐号Id
/// </summary>
public string UserId
{
get { return userInfo.uGuid; }
set
{
userInfo.uGuid = value;
OnPropertyChanged();
}
}
/// <summary>
/// 密码
/// </summary>
public string UserPwd
{
get { return userInfo.uPwd; }
set
{
userInfo.uPwd = value;
OnPropertyChanged();
}
}
/// <summary>
/// 登录按钮的命令处理
/// </summary>
public ICommand LoginCommand
{
get
{
return new RelayCommand(o =>
{
var handler = PendingBox.Show("正在登录中,请稍等...", "", false, null,new PendingBoxConfigurations()
{
LoadingForeground = "#5DBBEC".ToColor().ToBrush(),
ButtonBrush = "#5DBBEC".ToColor().ToBrush(),
LoadingSize = 50,
PendingBoxStyle = PendingBoxStyle.Classic,
FontSize = 15
});
var _name = "13166258391";
var _pwd = "13166258391";
if (!string.IsNullOrEmpty(_name) && !string.IsNullOrEmpty(_pwd))
{
var _uobj = new LoginUser { uName = _name, uPwd = _pwd };
var _res = userBLL.LoginAsync(_uobj).Result;
if (_res.statusCode != (int)ApiEnum.Status)
{
handler.Close();
Notice.Show("登录失败,请核实您的登录信息!", "提示", 3, MessageBoxIcon.Info);
Logger.Default.ProcessError((int)ApiEnum.Error, "登录失败-->账号【" + _name + "】密钥【" + _pwd + "】。");
return;
}
else
{
var _uobjinfo = _res.data;
if (_uobjinfo == null)
{
handler.Close();
Notice.Show("登录失败,请核实您的登录信息!", "提示", 3, MessageBoxIcon.Info);
Logger.Default.ProcessError((int)ApiEnum.Error, "登录信息不存在数据库中-->账号【" + _name + "】密钥【" + _pwd + "】。");
return;
}
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
handler.Close();
WindowManager.CloseWindow("loginWindow", o);//关闭登录页
}
}
else
{
Notice.Show("请确认您的登录信息输入是否完整!", "提示", 3, MessageBoxIcon.Info);
return;
}
});
}
}
}
}
3、登录过程
原创不易,打字截图不易,走过路过,不要错过,欢迎点赞,收藏,转载,复制,抄袭,留言,动动你的金手指,早日实现财务自由!