Current.SessionID 转换为 Guid
Current.SessionID convert to Guid
我需要根据 CurrentContext 的 SessionID 创建 Guid。该 Guid 存储在一些 TraceContext class 中,稍后将记录到 DB。
现在我正在使用 Guid.NewGuid() 创建新的 guid,但我想根据 sessionId 创建 guid。
我试过解析它:
var sessionId = HttpContext.Session.SessionId;
var result = Guid.Parse(sessionId);
问题是SessionId是24个字符的字符串,例如:
jeycop2ty20icvuvh2jks20g
Guid 必须是 32 个字符长,所以我得到 Parse 异常。
有什么方法可以从这个字符串创建 Guid 吗?
一个GUID
是一个[G]lobally [U]nique [ID]entifier,它不是哈希算法。
如果要将会话 ID 映射到 GUID,则必须进行映射 table 或类似操作。
或者,您可能会考虑,在会话首次创建时将 GUID 存储在会话中。
// Global.asax
public void Session_OnStart()
{
Session["GUIDSessionID"] = Guid.NewGuid().ToString();
}
然后在每次加载页面时阅读它。
var sessionId = HttpContext.Current.Session["GUIDSessionID"];
var result = Guid.Parse(sessionId);
这当然可以在没有从字符串来回转换的情况下完成。
这两种解决方案都是骇人听闻的,您应该考虑完全不使用 Guid 或将其添加到您的会话中(参见 André 的解决方案)。
解决方案 1
您可以覆盖 SessionIdManager
以提供 Guid 作为 SessionId
见SessionIDManager.CreateSessionID Method and Session Identifiers documentation (Custom Session Identifiers)。
来自 MDSN 库的代码:
自定义 SessionIDManager class
public class GuidSessionIDManager : SessionIDManager
{
public override string CreateSessionID(HttpContext context)
{
return Guid.NewGuid().ToString();
}
public override bool Validate(string id)
{
try
{
Guid testGuid = new Guid(id);
if (id == testGuid.ToString())
return true;
}
catch
{
}
return false;
}
}
在web.config中注入:
<sessionState
...
sessionIDManagerType="Your.Namespace.GuidSessionIDManager, YourAssemblyName"
/>
解决方案 2
default implementation of SessionIdManager构建一个15bytes的数组来生成一个字符串。 Guid 是 16 字节,因此您可以将字符串解码为 15 字节数组,然后从中构建一个 Guid,最后一个字节为 0。实施可能会在未来的版本中发生变化。
我需要根据 CurrentContext 的 SessionID 创建 Guid。该 Guid 存储在一些 TraceContext class 中,稍后将记录到 DB。
现在我正在使用 Guid.NewGuid() 创建新的 guid,但我想根据 sessionId 创建 guid。
我试过解析它:
var sessionId = HttpContext.Session.SessionId;
var result = Guid.Parse(sessionId);
问题是SessionId是24个字符的字符串,例如:
jeycop2ty20icvuvh2jks20g
Guid 必须是 32 个字符长,所以我得到 Parse 异常。
有什么方法可以从这个字符串创建 Guid 吗?
一个GUID
是一个[G]lobally [U]nique [ID]entifier,它不是哈希算法。
如果要将会话 ID 映射到 GUID,则必须进行映射 table 或类似操作。
或者,您可能会考虑,在会话首次创建时将 GUID 存储在会话中。
// Global.asax
public void Session_OnStart()
{
Session["GUIDSessionID"] = Guid.NewGuid().ToString();
}
然后在每次加载页面时阅读它。
var sessionId = HttpContext.Current.Session["GUIDSessionID"];
var result = Guid.Parse(sessionId);
这当然可以在没有从字符串来回转换的情况下完成。
这两种解决方案都是骇人听闻的,您应该考虑完全不使用 Guid 或将其添加到您的会话中(参见 André 的解决方案)。
解决方案 1
您可以覆盖 SessionIdManager
以提供 Guid 作为 SessionId
见SessionIDManager.CreateSessionID Method and Session Identifiers documentation (Custom Session Identifiers)。
来自 MDSN 库的代码:
自定义 SessionIDManager class
public class GuidSessionIDManager : SessionIDManager
{
public override string CreateSessionID(HttpContext context)
{
return Guid.NewGuid().ToString();
}
public override bool Validate(string id)
{
try
{
Guid testGuid = new Guid(id);
if (id == testGuid.ToString())
return true;
}
catch
{
}
return false;
}
}
在web.config中注入:
<sessionState
...
sessionIDManagerType="Your.Namespace.GuidSessionIDManager, YourAssemblyName"
/>
解决方案 2
default implementation of SessionIdManager构建一个15bytes的数组来生成一个字符串。 Guid 是 16 字节,因此您可以将字符串解码为 15 字节数组,然后从中构建一个 Guid,最后一个字节为 0。实施可能会在未来的版本中发生变化。