【问题标题】:Generating authentication header from azure table through objective-c通过objective-c从azure表生成身份验证头
【发布时间】:2012-05-30 11:54:03
【问题描述】:

我正在从 iCloud 获取数据,为此我需要生成一个标头(天蓝色表存储)。

我为此使用了下面的代码,它正在生成标题。但是当我在我的项目中使用这些标头时,它显示“确保授权标头的值正确形成,包括签名。”

我google了很多,尝试了很多代码,但都是徒劳的。

任何人都可以请帮助我解决我在此代码中出错的地方。

-(id)generat
{
    NSString *messageToSign = [NSString stringWithFormat:@"%@/%@/%@", dateString,AZURE_ACCOUNT_NAME, tableName];

    NSString *key = @"asasasasasasasasasasasasasasasasasasasasas==";

    const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];

    const char *cData = [messageToSign cStringUsingEncoding:NSUTF8StringEncoding];

    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];

    CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];

    NSString *hash = [Base64 encode:HMAC];

    NSLog(@"Encoded hash: %@", hash);

    NSURL *url=[NSURL URLWithString: @"http://my url"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request addValue:[NSString stringWithFormat:@"SharedKeyLite %@:%@",AZURE_ACCOUNT_NAME, hash] forHTTPHeaderField:@"Authorization"];

    [request addValue:dateString forHTTPHeaderField:@"x-ms-date"];

    [request addValue:@"application/atom+xml, application/xml"forHTTPHeaderField:@"Accept"];

    [request addValue:@"UTF-8" forHTTPHeaderField:@"Accept-Charset"];

    NSLog(@"Headers: %@", [request allHTTPHeaderFields]);

    NSLog(@"URL: %@", [[request URL] absoluteString]);
     return request; 
}

-(NSString*)rfc1123String:(NSDate *)date
{
    static NSDateFormatter *df = nil;
    if(df == nil)
    {
        df = [[NSDateFormatter alloc] init];
        df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
        df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
    }
    return [df stringFromDate:date];
}

【问题讨论】:

  • @meccan no.im 没有使用 ARC。你知道我的代码有什么问题。
  • 好的,所以你泄露了“HMAC”和“df”对象
  • @meccan 没关系。我现在正在做一个示例项目。

标签: iphone objective-c azure icloud


【解决方案1】:

您可能缺少换行符:

NSString *messageToSign = [NSString stringWithFormat:@"%@/%@/%@", dateString,AZURE_ACCOUNT_NAME, tableName];

我不了解 Objective-C,但我希望格式为 @"%@\n/%@/%@"(如果这就是字符串文字中的换行符在这种编程语言中的工作方式)。

另外,我认为您将需要这两个标题:

DataServiceVersion:1.0;NetFx
MaxDataServiceVersion:1.0;NetFx

编辑:我突然想到这些标头可能出现在您代码的另一部分中。您可能还需要 x-ms-version 标头(也可能出现在代码的其他地方)。 /编辑

我不能凭良心指出将存储密钥放在客户端(如手机)上是个坏主意。任何获得您的应用程序的人都可以提取密钥并使用它来删除您的所有数据、上传他们的私人 DVD 收藏等。您的存储密钥是一个秘密,因此不应将其分发给客户。

【讨论】:

    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 2021-10-11
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2016-05-06
    • 1970-01-01
    相关资源
    最近更新 更多