将字符串转换为System

将字符串转换为System

本文介绍了将字符串转换为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

推荐答案

引用:

我的数据库表字段是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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:54