开发者

Convert string value back to GUID value

i have a guid value that i store in my hidden variable. say for eg (303427ca-2a5c-df11-a391-005056b73dd7)

now how do i convert the value from thi开发者_Python百科s hidden field back to GUID value (because the method i would be calling expects a GUID value).

thank you.


Just use the overloaded constructor:

try
{
  Guid guid = new Guid("{D843D80B-F77D-4655-8A3E-684CC35B26CB}");
}
catch (Exception ex) // There might be a more appropriate exception to catch
{
  // Do something here in case the parsing fails.
}


You are making it pretty easy on an attacker by storing the Guid in a string. Trivial to find back in, say, the paging file. Store it in a Guid and kill two birds with one stone.


    string strGuid;
    strGuid = (your guid here);
    Guid guid = new Guid(strGuid);

For more info, MSDN


Guid has a constructor for string Guids.

Guid guid = new Guid(myStringGuid);


new Guid(myHiddenFieldString)


I think it can be done simply as following:

Guid MyGuid = new Guid(stringValue);


In .NET4 onwards you can also use:

Guid myGuid = Guid.Parse(myGuidString); 

Just a matter of coding preference, but some people find this more intuitive.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜