将字符串转换为system.guid [英] Convert string to system.guid

查看:130
本文介绍了将字符串转换为system.guid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2015企业版为ASP.Net Project的空模板。在这个项目中,我获取会话用户ID值并将其存储在int中,并将整数值赋予string以将id值转换为Guid ...我已创建转换类来转换它。它正在转换但不是以正确的方式。任何人都可以在这个问题上解释我



我尝试过:



转换类来自互联网本身

I am using Visual studio 2015 Enterprise edition for Empty Template of ASP.Net Project. In this project i am getting the session User id value and stored it in int and the value of the integer is given to string to convert the id value to the Guid... I have created conversion class to convert it. It is converting but not in the correct way. Can anyone explain me in this issue

What I have tried:

Conversion Class has taken from the internet itself

public static Guid IntToGuid(int value)
{
    byte[] bytes = new byte[16];
    BitConverter.GetBytes(value).CopyTo(bytes, 0);
    return new Guid(bytes);
}







int id = Convert.ToInt32(Session["UserID"].ToString());
// Convert integer 182 as a hex in a string variable
string hexValue = id.ToString("x32");
Guid myID = Conversion.IntToGuid(Convert.ToInt32(hexValue));





这里我的id值为示例1然后输出显示为

myID = 00000001-0000-0000-0000-000000000000



任何人都可以帮助我



Here my id value for example 1 then the output is displayed as for
myID = 00000001-0000-0000-0000-000000000000

Can anyone help me

推荐答案

问题是你不能将GUID存储在一个整数中:标准int是一个Int32 - 一个32位的数字,所以它可以存储从-2,147,483,648到2,147,483,647的值。 GUID是一个无符号的128位数,因此它可以存储0到340,282,366,920,938,463,463,374,607,431,768,211,455 - 这显然不适合!因此,您浪费了使用GUID的全部优势:它极不可能被复制。与数据库IDENTiTY字段不同,GUID不是数字的常规序列,如果您快速连续使用两个,则得到的值相差一个 - 使用GUID,您获得的值非常不同!



如果你想使用整数ID然后在整个过程中使用整数 - 不要试图假设它是一个有效的guid。

如果你想使用GUID ID ,然后在整个过程中使用GUID - 不要;尝试以任何其他格式存储它们。





The problem is that you can't store a GUID in an integer: a standard int is an Int32 - a 32 bit number, so it can store values from −2,147,483,648 to 2,147,483,647. A GUID is a unsigned 128 bit number, so it can store from 0 to 340,282,366,920,938,463,463,374,607,431,768,211,455 - which obviously won't fit! As a result, you waste the whole advantage of using a GUID: that it is extremely unlikely to be be duplicated. Unlike database IDENTiTY fields, a GUID is not a "regular sequence" of numbers such that if you use two in quick succession you get values which differ by one - with GUIDs you get values that are wildly different!

If you want to use integer ID's then use integers throughout - don't try to assume that it's a "valid guid".
If you want to use GUID ids, then use GUIDs throughout - dont; try to store them in any other format.


引用:

我的数据库表字段是UserID,用户名,密码和电子邮件

在此字段中

My DB Table fields are UserID, Username, Password and Email
In this fields

UserID is an integer datatype
Username is an varchar datatype
password is an varchar datatype
email is an varchar datatype

还有三件事要做:

1)在更改任何内容之前先检查其他表 - 如果它们包含旧ID的外键,那么您还需要向该表添加一列并将新GUID ID复制到其中,否则您将失去连接。

2)切勿以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ]它被认为是代码犯罪 [ ^ ]

3)使用NVARCHAR而不是VARCHAR - 它使用Unicode而不是ASCII,它与.NET代码匹配(这是所有Unicode)ASCII是一个小得多的字符集,如果你存储一个VARCHAR字段,一些用户名可能根本不起作用。您还应该将电子邮件视为自2012年以来允许使用的Unicode。

Three other things to do:
1) Check other tables first before you change anything - if they contain foreign keys to the old ID, then you need to add a column to that tables as well and copy the "new GUID ID" into them or you will lose the connection.
2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] It's considered a Code Crime[^]
3) Use NVARCHAR instead of VARCHAR - it uses Unicode instead of ASCII, which matches to .NET code (which is all Unicode) ASCII is a much smaller set of characters, and some usernames may not work at all if you store them an VARCHAR fields. You should also consider emails as Unicode as it has been allowed since 2012.


这篇关于将字符串转换为system.guid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆