如何在不使用owin,identity或jwt库的情况下在webapi中生成随机唯一且安全的令牌? [英] How to generate random unique and secure token in webapi without using owin, identity or jwt library?

查看:90
本文介绍了如何在不使用owin,identity或jwt库的情况下在webapi中生成随机唯一且安全的令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何为不同的网络和移动应用程序使用的网络API生成随机,唯一且安全的令牌。



我拥有什么试过:



研究了不同的技术

How we can generate random , unique and secure token for a web api that will be consumed by different web and mobile applications.

What I have tried:

reserached different techniques

推荐答案

你可以使用这个, RNGCryptoServiceProvider类(System.Security.Cryptography) [ ^ ]它提供了一个安全的随机数 - 而不是种子随机数。在大多数安全的情况下,这是应该使用的随机数生成器,而不是.NET框架的普通旧Random类。您可以在MSDN页面上阅读有关此对象的更多信息。



更新



如果您仅为了身份验证而使用令牌,例如OAuth等。那么我建议使用Guid对象,为每个设备创建一个新的唯一字符串。它的大小为128位(2 128 + 1)是一个巨大的数量。你也可以很容易地生成它,

You may use this, RNGCryptoServiceProvider Class (System.Security.Cryptography)[^] and it provides a secure random — not a seeded random number. In most secure cases this is the random number generator that should be used instead of the plain old Random class of the .NET framework. You can read more about this object on the MSDN page itself.

Update:

If you are using the token only for the sake of authentication, like OAuth etc. Then I would recommend using the Guid object, to create a new unique string for every device. It has a size of 128 bits (2128 + 1) is a huge amount. You can generate that quite easily as well,
var token = Guid.NewGuid().ToString();



然后可以在需要的地方存储和使用它。我在自己的应用程序中使用了这种方法。在一切都需要私有的情况下,生成令牌的安全和神秘方式将非常有用 - 现金管理是此处的示例之一,加密/解密或生成用于密码散列的安全密钥或盐。



有关详细信息,请参阅: Guid结构(系统) [ ^ ]


这篇关于如何在不使用owin,identity或jwt库的情况下在webapi中生成随机唯一且安全的令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆