Using Parts of GUID as ID

后端 未结 6 750
借酒劲吻你
借酒劲吻你 2021-01-18 21:40

I\'m developing an ASP .Net MVC application. One of my actions requires id as a parameter. For example:

public actionresult Detail(Guid id){
            


        
相关标签:
6条回答
  • 2021-01-18 22:16

    No, it's not safe.

    You can calculate a SHA-2 hash of it though, and take the first few characters of that.

    0 讨论(0)
  • 2021-01-18 22:20

    You can clean the GUID of -s and convert the HEX to Base32 (A-Z,0-5) which will shorten it to 16 characters.

    0 讨论(0)
  • 2021-01-18 22:21

    GUID is designed in such a way that it is intended to be unique, but any part of it is not. See this blog post for details. If you need to shorten the GUID take a good hash of it - like SHA-1 or (if you don't have security concerns) MD5.

    0 讨论(0)
  • 2021-01-18 22:22

    No, you need the entire GUID since there is a possibility that a subset may not be unique.

    For example:

    0c157b42-379d-41d5-b9ba-83e9df9985b2

    0c157b42-379d-41d5-b9ba-83e9df9985b3

    Notice, only the last number is different. The beginnings are both the same. You can't use the trailing end of the GUID either since there's no way to predict what part of the GUID will change when its created.

    0 讨论(0)
  • 2021-01-18 22:24

    Some other options to consider- * If there are more than one Details with GUIDs starting with 0c157b42, have the URL localhost/Detail/0c157b42 show a list of applicable Details objects. * URL aliasing - allow for a "Friendly URL" field on the Details object.

    0 讨论(0)
  • 2021-01-18 22:39

    Bit of a late response but in case anyone reads this...

    depending on the use, you can provided a shortened GUID value.

    for instance, if the ID value is generated and given to the user as an Authentication Token sort of value then during the generation you could just take however many characters and compare it with other in use values. if any matches, then just generate a new one and re-compare until its unique.

    This technique is also advisable if you trim a hash value of the GUID too.. just to be safe. In fact any time you randomly generate a value to be used as ID then you should make sure it is not 'already in use'

    0 讨论(0)
提交回复
热议问题