gpt4 book ai didi

ASP.net 敏感信息存储

转载 作者:行者123 更新时间:2023-12-04 05:15:52 25 4
gpt4 key购买 nike

我在使用 ASP.net 时遇到了一个小问题。我有一个小的数据表,我需要依赖页面并且用户无法访问。我的意思是:

  1. If i store the data from the DataTable inside a Hiddenfield, the hiddenfield is page dependent (different values for multiple same page requests) but its not user inaccessible since a user can modify its content and then post back.

  2. If i store the Datatable in session, that is user inaccessible (which is good) but since some of the content from the page depends on this value, if a user opens the page multiple times (in different tabs) then the session is updated with the content from the last page requested and therefore the "older" pages are not properly rendered if a postback occurs.



示例:取一个整数变量。这是敏感信息。我需要将此值保存到,以便用户无法修改它,并且每个页面也可以具有不同的值(与隐藏字段的工作原理相同)。我该怎么做?谢谢!

PS:我在 C# 中使用 ASP.net 4.0

最佳答案

为隐藏字段添加唯一键;使用此键访问特定于页面实例的唯一 session 值。即使有人猜到了其他人的唯一 key ,没有 session key 也将毫无用处。

示例:

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

第一次呈现页面实例时生成此值:
if( !Page.IsPostback ){
this.InstanceId.Value = GenerateKey().ToString();
}

从特定于该页面的 Session 检索值时:
string key = this.InstanceId.Value;
var value = Session[key];

要生成页面唯一 ID,可以使用以下方法:
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 );
}

请记住,该 session 不一定是 100% 安全的(例如 Session fixation 攻击),但它比将信息存储在发送给客户端的数据中要安全几个数量级。

关于ASP.net 敏感信息存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14285914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com