Skip to main content

自定义新用户账号密码插件

通过定义自定义用户账号密码插件,可以自由设置新用户的默认密码规则。该插件对重置密码同样生效。

脚本说明​

脚本应该返回用户的密码字符串。

基本示例​

using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Chameleon.Common;
using Chameleon.Common.Context;
using Chameleon.Common.Exceptions;
using Chameleon.Common.Models;
using Chameleon.Database;
using Chameleon.SDK;

public class ChameleonCSharpScript
{
public async Task<object> ExecuteAsync(ComponentContext context, ChameleonSDK sdk)
{
// any code to generate the default password
return "new password";
}
}

脚本参数​

脚本执行时,可以动态获取到用户相关的一些信息,可用这些信息去参与密码的生成。

参数信息可以通过 context.DicParam 字典获取。

内置配置项​

配置项说明
UserId用户 ID
UserName用户姓名
UserEmail用户邮箱
UserNumber用户编号
UserPhone用户手机号

使用示例​

以下是一个根据用户信息生成默认密码的示例:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Chameleon.Common;
using Chameleon.Common.Context;
using Chameleon.Common.Exceptions;
using Chameleon.Common.Models;
using Chameleon.Database;
using Chameleon.SDK;

public class ChameleonCSharpScript
{
public async Task<object> ExecuteAsync(ComponentContext context, ChameleonSDK sdk)
{
// 获取用户信息
var userNumber = context.DicParam.TryGetValue("UserNumber", out var number) ? number : "";
var userPhone = context.DicParam.TryGetValue("UserPhone", out var phone) ? phone : "";

// 根据用户编号后4位 + 手机号后4位生成密码
var password = $"Chameleon{number.Substring(Math.Max(0, number.Length - 4))}{phone.Substring(Math.Max(0, phone.Length - 4))}";

return password;
}
}

使用场景​

  • 根据用户信息生成固定规则的默认密码
  • 生成随机密码并通知用户
  • 对接外部密码管理系统
  • 实现密码复杂度策略

相关文档​