无法从Xamarin iOS SecKeychain检索GUID

时间:2017-07-21 10:15:35

标签: c# ios xamarin.ios

在我的Xamarin iOS应用程序中,我将DeviceId和AccessToken存储在钥匙串中。为了保存和检索访问令牌,我的代码工作正常,但是对于设备ID(它是GUID),在保存device-id时,它会将结果显示为DuplicateItem,并且在检索设备ID时,它会给出结果为ItemNotFound。我正在使用单独的密钥来设备ID和访问令牌。这是我的代码

public const string DEVICE_ID = "DEVICE_ID";
public const string ACCESS_TOKEN = "ACCESS_TOKEN";

public static string GetValue(string key)
{
    var query = new SecRecord(SecKind.GenericPassword)
    {
       Generic = NSData.FromString(key)
    };

    SecStatusCode result;
    var match = SecKeyChain.QueryAsRecord(query, out result);
    if (result == SecStatusCode.Success)
    {
       return match.ValueData.ToString();
    }
    return string.Empty;
}

public static void SetValue(string value, string key)
{
    var query = new SecRecord(SecKind.GenericPassword)
    {
       ValueData = NSData.FromString(value),
       Generic = NSData.FromString(key)
    };

    var result = SecKeyChain.Add(query);
}

我通过方法调用保存并获取设备ID,如

SetValue(Guid.NewGuid(), DEVICE_ID); 
string deviceId = GetValue(DEVICE_ID);

它可能看起来很奇怪,但我不知道它为什么会这样,有没有人遇到过这个问题或者我的代码中是否有任何错误。请帮帮我。

1 个答案:

答案 0 :(得分:2)

这是我们用于存储和检索SecKeyChain的类,我建议使用我们的类,看看你是否得到相同的结果。

using Security;
using Foundation;

public class KeyChain
{
    public const string DEVICE_ID = "DEVICE_ID";
    public const string ACCESS_TOKEN = "ACCESS_TOKEN";

    public string ValueForKey(string key)
    {
        var record = ExistingRecordForKey (key);
        SecStatusCode resultCode;
        var match = SecKeyChain.QueryAsRecord(record, out resultCode);

        if (resultCode == SecStatusCode.Success)
            return NSString.FromData (match.ValueData, NSStringEncoding.UTF8);
        else
            return String.Empty;
    }

    public void SetValueForKey(string value, string key) 
    {
        var record = ExistingRecordForKey (key);            
        if (value.IsNullOrEmpty())
        {
            if (!ValueForKey(key).IsNullOrEmpty())
                RemoveRecord(record);

            return;
        }

        // if the key already exists, remove it
        if (!ValueForKey(key).IsNullOrEmpty())
            RemoveRecord(record);

        var result = SecKeyChain.Add(CreateRecordForNewKeyValue(key, value));
        if (result != SecStatusCode.Success)
        {
            throw new Exception(String.Format("Error adding record: {0}", result));
        }
    }

    private SecRecord CreateRecordForNewKeyValue(string key, string value)
    {
        return new SecRecord(SecKind.GenericPassword)
        {
            Account = key,
            Service = ServiceName,
            Label = key,
            ValueData = NSData.FromString(value, NSStringEncoding.UTF8),
        };
    }

    private SecRecord ExistingRecordForKey(string key)
    {
        return new SecRecord(SecKind.GenericPassword)
        {
            Account = key,
            Service = ServiceName,
            Label = key,
        };
    }

    private bool RemoveRecord(SecRecord record)
    {
        var result = SecKeyChain.Remove(record);
        if (result != SecStatusCode.Success)
        {
            throw new Exception(String.Format("Error removing record: {0}", result));
        }

        return true;
    }        
}

然后,要从中检索信息的类和/或方法如下所示:

public class OtherClass
{
    Public void GetInfo()
    {
        // Store device id
        KeyChain.SetValueForKey(Guid.NewGuid(), KeyChain.DEVICE_ID);

        // Retrieve device id
        string value = KeyChain.ValueForKey(DEVICE_ID);

        // Store access token
        KeyChain.SetValueForKey(Guid.NewGuid(), KeyChain.ACCESS_TOKEN);

        // Retrie acce token
        string value = KeyChain.ValueForKey(ACCESS_TOKEN);
    }
}