ASP.net sensitive information storage

后端 未结 1 763
北荒
北荒 2020-12-20 03:33

I\'m having a small trouble with ASP.net. I have a small DataTable that i need to be page dependent and also user inaccessible. What i mean is:

相关标签:
1条回答
  • 2020-12-20 03:48

    Add a unique key to a hidden field; use this key to access a unique session value that is specific to the instance of the page. Even if someone guessed someone else's unique key(s), it would be useless without the session key.

    Example:

    <input type="hidden" value="234092735029730" id="InstanceId" runat="server" />
    

    Generate this value the first time the instance of the page is rendered:

    if( !Page.IsPostback ){
        this.InstanceId.Value = GenerateKey().ToString();
    }
    

    When retrieving a value from Session specific to that page:

    string key = this.InstanceId.Value;
    var value = Session[key];
    

    To generate a page-unique ID, something like this will work:

    using System.Security.Cryptography;
    
    private static RNGCryptoServiceProvider _crypto = new RNGCryptoServiceProvider();
    
    public static long GenerateKey(){
        byte[] bytes = new byte[8];
        _crypto.GetBytes( bytes );
        return BitConverter.ToInt64( bytes, 0 );
    }
    

    Keep in mind that that Session isn't necessarily 100% secure (e.g. Session fixation attacks) but it is orders of magnitude more secure than storing the information in the data sent to the client.

    0 讨论(0)
提交回复
热议问题