爸爸去哪儿快乐大本营,洞口县人民政府,门阀乱
asp.net core data protection 不仅提供了非对称加密能力,而且提供了灵活的秘钥存储方式以及一致的加解密接口(protect与unprotect)。session中用到了它,cookie验证中用到了它,openidconnect中也用到了它。。。当然你也可以在应用开发中使用它,比如这篇博文中就是用它生成激活帐户的验证token。
首先在 startup.configureservices() 中注册 dataprotection 服务(注入 idataprotectionprovider 接口的实现):
public void configureservices(iservicecollection services) { services.adddataprotection(); }
然后在使用 dataprotection 的类的构造函数中添加 idataprotectionprovider 接口,并用该接口创建 dataprotector ,接着以此创建 securedataformat ,最后用 securedataformat.protect() 方法生成激活帐户的 token ,用 securedataformat.uprotect() 解密 token,完整的示例代码如下:
public class homecontroller : controller { private readonly isecuredataformat<string> _dataformat; public homecontroller(idataprotectionprovider _dataprotectionprovider) { var dataprotector = _dataprotectionprovider.createprotector(typeof(homecontroller).fullname); _dataformat = new securedataformat<string>(new stringserializer(), dataprotector); } public string generatetoken() { return _dataformat.protect(guid.newguid().tostring() + ";" + datetime.now.addhours(10)); } public string decrypttoken(string token) { return _dataformat.unprotect(token); } private class stringserializer : idataserializer<string> { public string deserialize(byte[] data) { return encoding.utf8.getstring(data); } public byte[] serialize(string model) { return encoding.utf8.getbytes(model); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论