加密 URL 参数 return NULL 问题

Encrypt URL parameter return NULL issue

我正在尝试加密 ASP.NET 中的 URL 参数 ID。我用 IDataProtector 构建了一个简单的项目。 但是我在示例代码中指向的行中收到 Null 引用错误。

控制器:

 private readonly IDataProtector protector;

    public QRScanningController(IDataProtectionProvider dataProtectionProvider, DataPro dataPro)
    {          
            protector = dataProtectionProvider.Create(dataPro.EmpIdRoutValue);            
    }

    public QRScanningController()
    {
        //protector = dataProtectionProvider.Create(dataPro.EmpIdRoutValue);
    }
    
    public ActionResult Index()
    {
        
        DataPro dataProobj = new DataPro();
        dataProobj.id = 12131;
        dataProobj.encID = protector.Protect(Encoding.ASCII.GetBytes(dataProobj.id.ToString())); **//the issue in this line return Null value.**
        return View(dataProobj);
    }

    public ActionResult QRscan(string encID)
    {

        return View();
    }

Protect函数也接受一个字符串作为参数。因此无需将 Id 转换为字节。 将 Index 中的代码更改为如下所示:

using Microsoft.AspNetCore.DataProtection;

...
dataProobj.encID = protector.Protect(dataProobj.id.ToString());
...

这对我有用。