studio
2024.10
false
- 发行说明
- 入门指南
- 设置和配置
- 自动化项目
- 依赖项
- 工作流类型
- 控制流程
- 文件比较
- 自动化最佳实践
- 源代码控件集成
- 调试
- 日志记录
- 诊断工具
- 工作流分析器
- 变量
- 参数
- 导入的命名空间
- 编码自动化
- 基于触发器的 Attended 自动化
- 录制
- 用户界面元素
- 选取器
- 对象存储库
- 数据抓取
- 图像与文本自动化
- Citrix 技术自动化
- RDP 自动化
- VMware Horizon 自动化
- Salesforce 自动化
- SAP 自动化
- macOS 用户界面自动化
- ScreenScrapeJavaSupport 工具
- Webdriver 协议
- 扩展程序
- 测试套件 - Studio
- 故障排除

Studio 用户指南
上次更新日期 2025年2月5日
在编码自动化中使用双重身份验证
本教程演示如何添加为自动化生成双重身份验证代码的示例代码。以下是实施双重身份验证代码的方法:
- 将代码复制并粘贴到目标项目的 CS 文件中。
- 在项目中使用示例 NUPGK 文件。
选择使用示例 NUPKG 文件时,可以将双重身份验证添加为 XAML 文件中的活动。
提示:无论您选择将示例双重身份验证代码生成器集成到 CS 文件(用于编码自动化)还是 XAML 文件(用于低代码自动化)中,请记住,您可以在低代码自动化中调用编码自动化,反之亦然。有关混合自动化的更多信息,请访问创建混合自动化 - 结合编码工作流和低代码工作流。
要在编码自动化中使用双重身份验证示例,您可以将以下示例代码复制并粘贴到目标项目中的 CS 文件中。
注意:示例代码每 30 秒生成一个新的身份验证代码。例如,如果在 3 点 45 分 27 秒生成身份验证代码,则该代码的可用时间为 3 秒。然后,系统会在 30 秒标记处生成新代码。
using System;
using System.Collections.Generic;
using UiPath.CodedWorkflows;
using System.Security.Cryptography;
namespace GenerateTwoFactorAuthenticationCode
{
public class Workflow : CodedWorkflow
{
[Workflow]
public string Execute(string secretKey)
{
return AuthenticationCode(secretKey);
}
private string AuthenticationCode(string secret)
{
// Step 1: Decode the base32 secret key
byte[] key = Base32Decode(secret);
// Step 2: Calculate the time step (current time divided by 30 seconds)
long unixTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
long timeStep = unixTime / 30;
// Step 3: Convert time step to byte array (8 bytes, big-endian)
byte[] timeBytes = BitConverter.GetBytes(timeStep);
if (BitConverter.IsLittleEndian)
Array.Reverse(timeBytes);
// Step 4: Generate HMAC-SHA1 hash using the time step as message and secret key
using (HMACSHA1 hmac = new HMACSHA1(key))
{
byte[] hash = hmac.ComputeHash(timeBytes);
// Step 5: Extract dynamic binary code (4 bytes) from the hash
int offset = hash[hash.Length - 1] & 0x0F;
int binaryCode = (hash[offset] & 0x7F) << 24
| (hash[offset + 1] & 0xFF) << 16
| (hash[offset + 2] & 0xFF) << 8
| (hash[offset + 3] & 0xFF);
// Step 6: Modulo to get a 6-digit code
int otp = binaryCode % 1_000_000;
// Return the OTP as a zero-padded 6-digit string
return otp.ToString("D6");
}
}
// Base32 decoding function to get the byte array from the base32-encoded key
private static byte[] Base32Decode(string base32)
{
// Decode Base32-encoded string to byte array
const string base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
int bitBuffer = 0;
int bitBufferLen = 0;
var result = new List<byte>();
foreach (char c in base32.ToUpper())
{
if (c == '=') break;
int index = base32Chars.IndexOf(c);
if (index < 0) throw new ArgumentException("Invalid Base32 character");
bitBuffer = (bitBuffer << 5) | index;
bitBufferLen += 5;
if (bitBufferLen >= 8)
{
result.Add((byte)(bitBuffer >> (bitBufferLen - 8)));
bitBufferLen -= 8;
}
}
return result.ToArray();
}
}
}
using System;
using System.Collections.Generic;
using UiPath.CodedWorkflows;
using System.Security.Cryptography;
namespace GenerateTwoFactorAuthenticationCode
{
public class Workflow : CodedWorkflow
{
[Workflow]
public string Execute(string secretKey)
{
return AuthenticationCode(secretKey);
}
private string AuthenticationCode(string secret)
{
// Step 1: Decode the base32 secret key
byte[] key = Base32Decode(secret);
// Step 2: Calculate the time step (current time divided by 30 seconds)
long unixTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
long timeStep = unixTime / 30;
// Step 3: Convert time step to byte array (8 bytes, big-endian)
byte[] timeBytes = BitConverter.GetBytes(timeStep);
if (BitConverter.IsLittleEndian)
Array.Reverse(timeBytes);
// Step 4: Generate HMAC-SHA1 hash using the time step as message and secret key
using (HMACSHA1 hmac = new HMACSHA1(key))
{
byte[] hash = hmac.ComputeHash(timeBytes);
// Step 5: Extract dynamic binary code (4 bytes) from the hash
int offset = hash[hash.Length - 1] & 0x0F;
int binaryCode = (hash[offset] & 0x7F) << 24
| (hash[offset + 1] & 0xFF) << 16
| (hash[offset + 2] & 0xFF) << 8
| (hash[offset + 3] & 0xFF);
// Step 6: Modulo to get a 6-digit code
int otp = binaryCode % 1_000_000;
// Return the OTP as a zero-padded 6-digit string
return otp.ToString("D6");
}
}
// Base32 decoding function to get the byte array from the base32-encoded key
private static byte[] Base32Decode(string base32)
{
// Decode Base32-encoded string to byte array
const string base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
int bitBuffer = 0;
int bitBufferLen = 0;
var result = new List<byte>();
foreach (char c in base32.ToUpper())
{
if (c == '=') break;
int index = base32Chars.IndexOf(c);
if (index < 0) throw new ArgumentException("Invalid Base32 character");
bitBuffer = (bitBuffer << 5) | index;
bitBufferLen += 5;
if (bitBufferLen >= 8)
{
result.Add((byte)(bitBuffer >> (bitBufferLen - 8)));
bitBufferLen -= 8;
}
}
return result.ToArray();
}
}
}
如果要使用示例 NUPKG 文件在自动化中包含双重代码,请按照以下步骤操作:
- 下载以下 NUPKG 文件:GenerateTwoFactorAuthenticationCode.nupkg。
- 将下载的 NUPKG 文件上传到 Orchestrator 主机或租户订阅源,您可通过 Studio 实例访问这些订阅源。
有关将 NUPKG 文件作为自定义库上传到 Orchestrator 的更多信息,请访问手动将库上传到 Orchestrator。
- 打开 Studio 项目,然后打开“管理包”菜单。
- 搜索您先前保存到 Orchestrator 主机或 Orchestrator 租户订阅源的
GenerateTwoFactorAuthenticationCode
NUPKG 文件,并安装该文件。图 1.“管理包”菜单中的自定义库
安装该文件后,请导航至“活动”面板并找到“生成双重身份验证代码”。将“获取 2FA 令牌”活动拖放到 XAML 文件中,以便为自动化生成身份验证代码。
图 2.“活动”面板中的“生成双重身份验证”代码示例