studio
2024.10
false
UiPath logo, featuring letters U and I in white

Studio 用户指南

上次更新日期 2025年2月5日

在编码自动化中使用双重身份验证

本教程演示如何添加为自动化生成双重身份验证代码的示例代码。以下是实施双重身份验证代码的方法:
  • 将代码复制并粘贴到目标项目的 CS 文件中。
  • 在项目中使用示例 NUPGK 文件。

    选择使用示例 NUPKG 文件时,可以将双重身份验证添加为 XAML 文件中的活动。

提示:无论您选择将示例双重身份验证代码生成器集成到 CS 文件(用于编码自动化)还是 XAML 文件(用于低代码自动化)中,请记住,您可以在低代码自动化中调用编码自动化,反之亦然。有关混合自动化的更多信息,请访问创建混合自动化 - 结合编码工作流和低代码工作流

将代码复制并粘贴到 CS 文件中

要在编码自动化中使用双重身份验证示例,您可以将以下示例代码复制并粘贴到目标项目中的 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();
        }
    }
}

使用示例 NUPGK 文件

如果要使用示例 NUPKG 文件在自动化中包含双重代码,请按照以下步骤操作:
  1. 下载以下 NUPKG 文件:GenerateTwoFactorAuthenticationCode.nupkg
  2. 将下载的 NUPKG 文件上传到 Orchestrator 主机租户订阅源,您可通过 Studio 实例访问这些订阅源。

    有关将 NUPKG 文件作为自定义库上传到 Orchestrator 的更多信息,请访问手动将库上传到 Orchestrator

  3. 打开 Studio 项目,然后打开“管理包”菜单。
  4. 搜索您先前保存到 Orchestrator 主机Orchestrator 租户订阅源的 GenerateTwoFactorAuthenticationCode NUPKG 文件,并安装该文件。
    图 1.“管理包”菜单中的自定义库

安装该文件后,请导航至“活动”面板并找到“生成双重身份验证代码”。将“获取 2FA 令牌”活动拖放到 XAML 文件中,以便为自动化生成身份验证代码。
图 2.“活动”面板中的“生成双重身份验证”代码示例

  • 将代码复制并粘贴到 CS 文件中
  • 使用示例 NUPGK 文件

此页面有帮助吗?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath Logo White
信任与安全
© 2005-2025 UiPath。保留所有权利。