asp.net中是否有唯一标识符来区分同一页面的多个选项卡/浏览器窗口?
假设我有一个处理用户上传的文件的网站(例如,存储或从sql表中提取数据)。是否有办法确保一个用户可以打开多个选项卡/浏览器窗口,每个选项卡/浏览器窗口编辑不同的文件 我可以使用会话ID作为解决此问题的方法吗?我还需要避免这里提到的超时问题,但据我所知,身份验证票据在同一浏览器的所有选项卡/窗口中共享asp.net中是否有唯一标识符来区分同一页面的多个选项卡/浏览器窗口?,asp.net,session,Asp.net,Session,假设我有一个处理用户上传的文件的网站(例如,存储或从sql表中提取数据)。是否有办法确保一个用户可以打开多个选项卡/浏览器窗口,每个选项卡/浏览器窗口编辑不同的文件 我可以使用会话ID作为解决此问题的方法吗?我还需要避免这里提到的超时问题,但据我所知,身份验证票据在同一浏览器的所有选项卡/窗口中共享 谢谢没有这样的事 每个选项卡通常是同一会话的一部分,但浏览器不需要将正在使用的选项卡传递回服务器(没有这样做的规范) 既然网络被认为是无状态的,那么这样做就无法达到这个目的 您可以通过将自己的标识符
谢谢没有这样的事 每个选项卡通常是同一会话的一部分,但浏览器不需要将正在使用的选项卡传递回服务器(没有这样做的规范) 既然网络被认为是无状态的,那么这样做就无法达到这个目的
您可以通过将自己的标识符(标题、隐藏表单字段等)嵌入到特定的请求/响应对中来识别它。出于我的需要,我决定将文件名另存为字符串,以便在不同的选项卡/浏览器窗口上进行识别。该字符串使用querystring在网页之间传递,我在存储该值的数据库表中添加了一个新列
对数据库表所做的任何更改都将使用文件名作为筛选器。因此,不同的页面(具有不同的查询字符串)将能够同时处理数据库表,而不会相互干扰。希望能够帮助其他人处理唯一标识符,以便在服务器端的ASP.NET中进行验证。这是我的解决办法
/// <summary>
/// Page Load event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Check if session object has not been assigned - page is being loading first time or session has been expired
if (Session["ValidateId"] == null)
Session["ValidateId"] = Session.SessionID;
}
else
{
//Check if ViewState has not been previously assigned
if (ViewState["UniqueId"] == null)
{
//Always store ticks when page is being requested through post back
Int64 numberOfTicks = 0;
numberOfTicks = DateTime.Now.Ticks;
//keep this unique id to current page between post backs
ViewState["UniqueId"] = Session.SessionID + "_" + numberOfTicks.ToString();
}
}
}
/// <summary>
/// button click event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnCalculate_Click(object sender, EventArgs e)
{
if (Session["ValidateId"] == null)
{
SetAlertMessage("Current Session is Expired. Please reload the page to continue.");
this.lblValidate.Text = "Current Session is Expired. Please reload the page to continue.";
return;
}
else
{
//Assign Unique Id from View State - id is unique across browser windows and belong only to current page
Session["UniqueId"] = ViewState["UniqueId"];
//Instantiate object to run some calculation and manipulate records in database
this._transformerloadParser = new TransformerLoadDataParser(ViewState["UniqueId"].ToString());
}
}
/// <summary>
/// User alert message through ClientScriptManager
/// </summary>
/// <param name="message"></param>
protected void SetAlertMessage(String message)
{
StringBuilder sb = null;
String scName = "AlertScript";
ClientScriptManager csm = Page.ClientScript;
Type scType = Page.GetType();
sb = new StringBuilder();
sb.Append("alert('");
sb.Append(message);
sb.Append("')");
ScriptManager.RegisterStartupScript(this, scType, scName, sb.ToString(), true);
}
//class logic
public class TransformerLoadDataParser
{
//Constructor
public TransformerLoadDataParser(String uniqueId)
{
Init(uniqueId);
}
/// <summary>
/// All required variable initialization
/// </summary>
protected void Init(String uniqueId)
{
try
{
this._userIdentityName = HttpContext.Current.User.Identity.Name;
if (HttpContext.Current.Session["UniqueId"] == null || !String.Equals(HttpContext.Current.Session["UniqueId"],uniqueId))
throw new Exception("UniqueId as Session Key has not been set, expired or not properly set.");
this._sessionId = uniqueId;
}
else
{
throw new Exception("Application settings are not defined in web config file.");
}
}
catch (Exception)
{
throw;
}
}
//some other logic
}
//
///页面加载事件处理程序
///
///
///
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
//检查是否尚未分配会话对象-第一次加载页面或会话已过期
if(会话[“ValidateId”]==null)
会话[“ValidateId”]=Session.SessionID;
}
其他的
{
//检查之前是否未指定ViewState
if(ViewState[“UniqueId”]==null)
{
//当通过回发请求页面时,始终存储勾号
Int64 numberOfTicks=0;
numberOfTicks=DateTime.Now.Ticks;
//在回发之间将此唯一id保留到当前页面
ViewState[“UniqueId”]=Session.SessionID+“”+numberOfTicks.ToString();
}
}
}
///
///按钮单击事件处理程序
///
///
///
受保护的无效btnCalculate\u单击(对象发送者,事件参数e)
{
if(会话[“ValidateId”]==null)
{
SetAlertMessage(“当前会话已过期。请重新加载页面以继续。”);
this.lblValidate.Text=“当前会话已过期。请重新加载页面以继续。”;
返回;
}
其他的
{
//从视图状态分配唯一Id-Id在浏览器窗口中是唯一的,并且只属于当前页面
会话[“UniqueId”]=ViewState[“UniqueId”];
//实例化对象以运行某些计算并操作数据库中的记录
此._transformerloadParser=新TransformerLoadDataParser(ViewState[“UniqueId”].ToString());
}
}
///
///通过ClientScriptManager发送的用户警报消息
///
///
受保护的void SetAlertMessage(字符串消息)
{
StringBuilder sb=null;
字符串scName=“AlertScript”;
ClientScriptManager csm=Page.ClientScript;
类型scType=Page.GetType();
sb=新的StringBuilder();
某人加上(“警告(”);
某人附加(信息);
某人加上(“)”;
ScriptManager.RegisterStartupScript(this,scType,scName,sb.ToString(),true);
}
//类逻辑
公共类TransformerLoadDataParser
{
//建造师
公共TransformerLoadDataParser(字符串唯一ID)
{
Init(uniqueId);
}
///
///所有必需的变量初始化
///
受保护的void Init(字符串唯一ID)
{
尝试
{
这是._userIdentityName=HttpContext.Current.User.Identity.Name;
if(HttpContext.Current.Session[“UniqueId”]==null | | |!String.Equals(HttpContext.Current.Session[“UniqueId”],UniqueId))
抛出新异常(“UniqueId作为会话密钥未设置、过期或未正确设置”);
此._sessionId=uniqueId;
}
其他的
{
抛出新异常(“web配置文件中未定义应用程序设置”);
}
}
捕获(例外)
{
投掷;
}
}
//其他一些逻辑
}
我不知道您的具体问题是什么,但为什么不在每个页面呈现上生成一个GUID?如果我使用一个标识符,例如标题,那么在导航到另一个页面并更新该页面的标题时是否需要检索标题?@Tony-我不知道您的意思。