简体   繁体   中英

Swift 2.0 with Alamofire: How to send http headers with X-WSSE Authorization?

Here is my code:

>   let uuid: CFUUIDRef = CFUUIDCreate(nil)
    let nonce: CFStringRef = CFUUIDCreateString(nil, uuid)
    let dateFormatter: NSDateFormatter = NSDateFormatter()
    let timestamp = NSDate()
    let secretKey = "DAqE6lBUgzxJRuR287GR"
    let username = "us_demo003"

    let formattedDate: String = dateFormatter.stringFromDate(timestamp)


    let text = String((nonce as String)+formattedDate+secretKey)

    let sha1 = text.sha1()

    let passwordDigest = sha1.toBase64()

    let headers = ["Username":username,"PasswordDigest": passwordDigest, "Nonce":(nonce as String), "Created":formattedDate]

    let theUrlString = "url here"

    manager.request(.GET, theUrlString, parameters: nil, encoding: ParameterEncoding.URL, headers: theHeaders).responseJSON { (result) -> Void in
        print("BEGIN")
        print("\n\n\n\n\n\n\n\nBEGIN\n\(result)\n\n\n\n\n\nEND")
        print("STOP")
    }

The result I'm getting is the following:

SUCCESS: {
data = "";
replyCode = 1;
replyText = "No Authentication Header";

}

How exactly do I send an authentication header with Alamofire? I've checked the documentation on the github. Something I'm not doing right.

Thanks

Instead you want to create the Authorization header where Authorization is the dictionary key and WWSE profile=\\"username_token\\" is the value. Then you also need to set the X-WSSE header key to UsernameToken Username="admin", PasswordDigest="buctlzbeVflrVCoEfTKB1mkltCI=", Nonce="ZmMzZDg4YzMzYzRmYjMxNQ==", Created="2014-03-22T15:24:49+00:00" .

let token = "some generated token"

let headers = [
    "Authorization": "WWSE profile=\"\(token)\""
    "X-WSSE": "UsernameToken Username=\"admin\", PasswordDigest=\"buctlzbeVflrVCoEfTKB1mkltCI=\", Nonce=\"ZmMzZDg4YzMzYzRmYjMxNQ==\", Created=\"2014-03-22T15:24:49+00:00\""
]

More info about WSSE auth can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM