【问题标题】:Secure Bearer Token using Encryption algorithm使用加密算法保护承载令牌
【发布时间】:2019-01-04 08:13:55
【问题描述】:

在使用 Identity 框架和 Owin 的 WebApi 项目中,授权和身份验证工作正常。下一步是我想让我的不记名令牌更安全,并且需要实现不记名令牌的加密和解密。

加密的字符串将在验证之前在 Actionfilter 中解密。有什么推荐的方法吗?我尝试了 AES 和 DES 的多种实现,但我正在尝试实现任何推荐的方法。

AES 实施:

static byte[] Encrypt(string plainText, byte[] Key, byte[] IV) {  
        byte[] encrypted;  
        // Create a new AesManaged.    
        using(AesManaged aes = new AesManaged()) {  
            // Create encryptor    
            ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);  
            // Create MemoryStream    
            using(MemoryStream ms = new MemoryStream()) {  
                // Create crypto stream using the CryptoStream class. This class is the key to encryption    
                // and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream    
                // to encrypt    
                using(CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) {  
                    // Create StreamWriter and write data to a stream    
                    using(StreamWriter sw = new StreamWriter(cs))  
                    sw.Write(plainText);  
                    encrypted = ms.ToArray();  
                }  
            }  
        }  
        // Return encrypted data    
        return encrypted;  
    }  
    static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV) {  
        string plaintext = null;  
        // Create AesManaged    
        using(AesManaged aes = new AesManaged()) {  
            // Create a decryptor    
            ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);  
            // Create the streams used for decryption.    
            using(MemoryStream ms = new MemoryStream(cipherText)) {  
                // Create crypto stream    
                using(CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) {  
                    // Read crypto stream    
                    using(StreamReader reader = new StreamReader(cs))  
                    plaintext = reader.ReadToEnd();  
                }  
            }  
        }  
        return plaintext;  
    }  

【问题讨论】:

    标签: c# asp.net-web-api encryption oauth


    【解决方案1】:

    是的。推荐的方法是不要这样做。应该没有理由这样做。

    记住 OAuth/OpenID 存在的原因。重点是去中心化身份验证。这就是为什么这些标记是 base64 编码的。这样,任何客户端都可以使用该第三方来识别用户。 (注意我没有写“授权”)显然用户应该给予同意,否则基于身份提供者拒绝访问。 (通过验证重定向 url,或者不知道客户端)

    还要注意 openId 和 OAuth 之间的区别。声明是一个 openID 概念。 Json webtokens 的概念也来自 OpenID(不是 OAuth)。 OAuth 没有可读的不记名令牌。再次:注意两种不同协议的目的。我试图在这里解释这些协议:https://medium.com/@abstarreveld/oauth-and-openid-explained-with-real-life-examples-bf40daa8049f

    对于您的问题:不要加密不记名令牌。您可以考虑使用引用令牌而不是 jwt 令牌。这样一来,阅读这些令牌的内容就会变得更加困难。如果您真的希望令牌不可读,请创建自己的 JUST OAUTH 实现。在那里使用范围而不是声明。

    希望这会有所帮助!

    【讨论】:

    • 该项目非常保密,不会有其他第三方参与。只有一个客户端和一个服务器。使用范围是个好主意,我们会尝试,但加密令牌是我们现在需要的。无论如何,我们希望我们的令牌对其他人来说是不可读的。
    • 我假设您使用的是 json 网络令牌,对吧?如果是这样:为什么不切换到参考令牌?这些是不可读的(除非你当然有秘密和自省端点......)
    • 我没有使用 JWT 令牌。我正在使用默认 OWIN 身份验证,这可能是 OAuth。
    • 啊..我明白了...那么您使用的是普通 OAuth...正如我在文章中描述的那样,OAuth 和 OpenID 之间的区别在于 OpenID 引入了声明和 json 的概念网络令牌(应该是可读的)。这很重要,因为您可能正在查看的 claimidentity 对象是 OpenID 而不是 OAuth 的实现。 Oauth 使用不可读的范围。 Oauth 并没有规定访问令牌是否应该是可读的。要实现这一点,您必须构建自己的 owin 实现。
    • 不过,重点是.. 编写自己的实现将变得更加困难,因为 OpenID(以及带有它的可读内容)被强烈嵌入在 .net 框架中。朝着这个方向前进 - 在我看来 - 雄心勃勃..
    猜你喜欢
    • 1970-01-01
    • 2017-01-09
    • 2017-03-19
    • 2019-12-15
    • 2013-07-20
    • 2014-12-18
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多