简体   繁体   中英

Updating a Password in RackSpace using C#

I'm just trying to change a password of the main account and a sub user in RackSpaceCloud using C# but I keep running into a UserNotAuthorized exception. Its weird because I can do anything else without this error, reset Api keys, list users and userID's(etc.). Sample Code

 net.openstack.Core.Domain.CloudIdentity cloudIdentity = new CloudIdentity()//Admin Credits
 {
     Username = "me",
     APIKey = "blahblahblah",
 };
 CloudIdentityProvider cloudIdentityProvider = new CloudIdentityProvider(cloudIdentity);
 cloudIdentityProvider.SetUserPassword("correctUserID", "newP@ssw0rd", cloudIdentity);

And then I error which is confusing because methods like,

cloudIdentityProvider.ListUsers(cloudIdentity)
cloudIdentityProvider.ResetApiKey("UserID", cloudIdentity);

Work Perfectly. Any Help or Ideas would be appreciated. Oh and Btw the addition info on the exception is always the same. "Unable to authenticate user and retrieve authorized service endpoints"

This is a bug. I have opened issue 528 but in the meantime here is a workaround.

var cloudIdentity = new CloudIdentity
{
    Username = "{username}",
    APIKey = "{api-key}"
};

var cloudIdentityProvider = new CloudIdentityProvider(cloudIdentity);
var userAccess = cloudIdentityProvider.Authenticate(cloudIdentity);

var request = new HttpRequestMessage(HttpMethod.Post, string.Format("https://identity.api.rackspacecloud.com/v2.0/users/{0}", userAccess.User.Id));
request.Headers.Add("X-Auth-Token", userAccess.Token.Id);

var requestBody = JObject.FromObject(new { user = new { username = userAccess.User.Name } });
((JObject)requestBody["user"]).Add("OS-KSADM:password", "{new-password}");
request.Content = new StringContent(requestBody.ToString(), Encoding.UTF8, "application/json");

using (var client = new HttpClient())
{
    var response = client.SendAsync(request).Result;
}

The cloud identity used must be an admin if you need to change another user's password, otherwise non-admins may only change their own password.

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