Skip to main content

自定义用户登录插件

通过定义自定义用户登录插件,可以扩展系统的登录能力,自定义新的第三方登录方式。

脚本说明​

用户自定义登录插件的脚本应该返回用户 ID 字符串,系统会根据用户 ID 自动生成 token 完成登录。

基本示例​

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 obtain the user id
return "d66c2f4f-db75-49bf-ad4d-515868b59e7f";
}
}

配置项​

配置项可定义一些标识,用于辅助登录。配置项会自动透传到脚本中的 context.DicParam 参数中,可以方便获取到自定义的配置项。

除自定义的任意配置项外,系统定义了一些内置的配置项,用来完成系统交互。

内置配置项​

配置项说明
Title第三方登录图标显示的标题
RedirectUrl第三方登录按钮点击时跳转地址
AutoJump是否自动跳转。如果任意一个启用的登录插件设置值=true,则用户在进入登录页面时,自动跳转到 RedirectUrl,而无需点击登录图标
Icon自定义第三方登录图标。可以使用 Ant Design 图标库 提供的图标,值填写图标名称字符串,例如:AlipayOutlined
Action交互操作,用于区分登录或注销操作。可选值:Login、Logout

完整示例​

以下是一个完整的登录脚本示例:

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)
{
// get parameters from query
// var query = ChameleonContext.Current.GetQuery();

// login action
if (context.DicParam.TryGetValue("Action", out var action) && action == "Login")
{
// can use httpclient to call thirdparty API to login and get user number or email
// var httpClient = sdk.HttpClientFactory.CreateClient("x");

// query the user information id from database by some thirdparty information
// login and return the user id
return "d66c2f4f-db75-49bf-ad4d-515868b59e71";
}
// logout action
else if (action == "Logout")
{
// redirect thirdparty logout path if needed
throw new ChameleonException("")
{
Action = Chameleon.Common.Enums.ActionEnum.Redirect,
RedirectUrl = "https://xxx.cn/cas/logout"
};
}
else
{
throw new ChameleonException("Unknown Action");
}
}
}

注意事项​

warning
  • 如果想要退出的时候成功回调,必须保证正确采用了 key 参数,因为 key 参数决定了上次登录所采用的插件(会在登录时记录上次登录所用的渠道),退出的时候,会明确进入上次登录时的插件
  • 退出时,会按上次登录的方式进行退出。例如:如果用系统级账号密码登录,则退出时,不会执行自定义插件

相关文档​