2014-04-27 102 views
27

如何在Windows Phone 8.1中獲取設備唯一ID?使用DeviceExtendedProperties.GetValue("DeviceUniqueId")的舊方式不適用於Windows通用應用。Windows Phone 8.1中的設備唯一ID

+0

任何示例?我在這裏問這個問題,以及:http://stackoverflow.com/questions/36004003/windows-phone-device-unique-id –

回答

23

請注意,當您編寫通用應用程序時,它不僅可以安裝在手機上。雖然在電話上技術上硬件配置是相同的,但在其他設備上,它可以改變,因此它的ID。這是我認爲沒有這種通用的方法來獲得ID。 (更多信息,你可以找到also here)。

您可以看看HardwareIdentification class及其方法GetPackageSpecificToken

HardwareToken myToken = HardwareIdentification.GetPackageSpecificToken(null); 
IBuffer hardwareId = myToken.Id; 

還有一個Guidance on using the App Specific Hardware ID (ASHWID) to implement per-device app logic

+2

如果你想使用它,你必須記住,這個ID會改變,當你更改應用程序簽名證書 – Johniak

+0

@Johniak謝謝你的支持。 – Romasz

+0

@Romasz'HardwareIdentification.GetPackageSpecificToken'取決於Package id/name。是否有任何其他ID對於安裝在同一設備上的所有應用程序顯示相同? –

31
private string GetDeviceID() 
{ 
    HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null); 
    IBuffer hardwareId = token.Id; 

    HashAlgorithmProvider hasher = HashAlgorithmProvider.OpenAlgorithm("MD5"); 
    IBuffer hashed = hasher.HashData(hardwareId); 

    string hashedString = CryptographicBuffer.EncodeToHexString(hashed); 
    return hashedString; 
} 

希望得到這個幫助!

+0

爲什麼MD5呢?爲什麼不使用其他算法? – Apoorva

+9

我會將「MD5」替換爲[HashAlgorithmNames.Md5](http://msdn.microsoft.com/en-us/library/windows.security.cryptography.core.hashalgorithmnames.md5.aspx)。 –

+0

您可以用Convert.ToBase64String替換CryptographicBuffer.EncodeToHexString,它會生成更短但仍然可讀的字符串。 – Grigory