views:

128

answers:

3

What s the best way to implement a method that creates and assings ID s to user on a asp.net application?

I was thinking about using DateTime ticks and thread id

I wanna make sure that there is no collision and user ids are unique.

ID can be a string or long.

should i use MD5 on some information that i collect from user? what would that be?

I have seen that md5 collision rate is very low.

+7  A: 

I would use GUIDs based off the limited information you've given.

ShaneC
using GUID guarantees uniqueness?
GUIDs are not always be best for IDs, particularly if they're the primary key in a database, but given the way the original question is worded, I have to agree.
Randolpho
Yes. As per http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx "The chance that the value of the new Guid will be all zeros or equal to any other Guid is very low."
Reddog
I will keep the ID in the cookie and in the cache.
How about performance of creating a GUID? is it efficient?
@Randolpho: We are using GUIDs as the primary keys in our DB for lots of our data tables - could you please explain or provide source which explains why they are not the best choice. Also, what would be a better choice?
VoodooChild
@user1777883: Yes, you will not notice any performance hit by using GUIDs
VoodooChild
Microsoft uses GUIDs as the Primary Key in their ASP.NET Provider Database....
Greg
@VoodooChild: "GUIDs are commonly used as the primary key of database tables, and with that, often the table has a clustered index on that attribute. This presents a performance issue when inserting records because a fully-random GUID means the record may need to be inserted anywhere within the table rather than merely appended near the end of it." (http://en.wikipedia.org/wiki/Globally_Unique_Identifier#Sequential_Algorithms)
Ben S
@user1777883: Regarding "using GUID guarantees uniqueness?" - GUID stands for Globally Unique IDentifier by the way. So it's great for ensuring your ID is not just unique to your application, but (most likely) everywhere else too (e.g. multiple web servers hosting your app).
Reddog
I m not storing GUID in a relational database.
GUIDs are not unique. There is a chance that duplicate ones can be generated, just like there's a change that hashes can collide.
blowdart
@VoodooChild, @Greg: as @Ben S points out, GUIDs as primary keys cause severe performance problems if there's a clustered index on the (GUID) primary key. GUIDs can be useful as primary keys in a database, particularly if it's a distributed database or one designed to work well with a UI that offers an "offline mode", but you have to be aware of the clustered index issue or you're gonna have problems. *Usually* a GUID is best avoided as the primary key of a table. It's one of those "feel free to break the rule if you happen to know what you're doing" rules.
Randolpho
http://www.informit.com/articles/article.aspx?p=25862Good article on GUIDs as Primary Keys but if you're using an ORM like NHibernate you can use guid.comb which will create fairly sequential guids to get around the performance problem.
ShaneC
@ShaneC: if you have to use sequential GUIDs, you're better off using autoincremented ints.
Randolpho
+3  A: 

The simplest solution is an autoincremented number. This requires a central server.

Date/time plus a one-way hash are for pseudo-random IDs. Do they have to be pseudo random for security? This should not be relied upon for uniqueness because by definition one-way hashes collide. You'd still need a central server to check for duplicates before issuing the ID.

GUIDs are best if the IDs are created in a distributed system (no central server to generate the ID). GUIDs can be generated on separate machines, and they shouldn't collide. Depends on the implementation, but some GUID algorithms are simply pseudo-random, and yes, there is still a possibility of collision.

Marcus Adams
A: 

Guid is by far the best choice for generating unique ids for something like a userid. They are absolutely guaranteed to be unique globally (hence the name). In order to best work with a clustered index you should use NEWSEQUENTIALID(). This generates sequential ids that can be appended to the index, and prevents sql server having to reorganise and page the index every time a value is added. There is a small security concern associated with using this function in that the next value in the sequence can be determined.

TheCodeKing
GUIDs are not "absolutely guaranteed" to be globally unique. Microsoft states that their implementation has "a very low probability of being duplicated". http://msdn.microsoft.com/en-us/library/system.guid.aspx
Marcus Adams
Microsoft also say they are globally unique: http://msdn.microsoft.com/en-us/library/ms190215(SQL.90).aspx"no other computer in the world will generate a duplicate of that GUID value."...however we are talking about different things. You are referring to .Net implementation where as I'm referring to the Sql Server implementation; hence we are both right.
TheCodeKing