Skip to content

Rackspace Identity Code Samples

Alan Quillin edited this page May 22, 2013 · 5 revisions

Identity service (keystone)

Since all service endpoints use the identity service for authentication and authorization, we will start here.

ScreenShot

User Authentication

Authenticate with username and password

//Required namespaces
using net.openstack.Providers.Rackspace;
using net.openstack.Core.Providers;
using net.openstack.Core.Exceptions.Response;

//Code snippet
try
{
    IIdentityProvider identityProvider = new CloudIdentityProvider();
    var userAccess = identityProvider.Authenticate(new RackspaceCloudIdentity
                                                       {
                                                           Username = "MyUserName", 
                                                           Password = "MyPassword"
                                                       });
}
catch(ResponseException ex2)
{
    // do something
}
Authenticate with username and API key
try
{
    IIdentityProvider identityProvider = new CloudIdentityProvider();
    var userAccess = identityProvider.Authenticate(new RackspaceCloudIdentity
                                                       {
                                                           Username = "MyUserName", 
                                                           APIKey = "MyAPIKey"
                                                       });
}
catch(ResponseException ex2)
{
    // do something
}

By default, the US (default) cloud instance is used (This is the instance for all Cloud Regions EXCEPT LON. All future cloud instances will be in US (default) the cloud region). If you need to target the LON cloud instance, you only need to pass in the CloudInstance.UK

Authenticate at the LON cloud instance.
try
{
    IIdentityProvider identityProvider = new CloudIdentityProvider();
    var userAccess = identityProvider.Authenticate(new RackspaceCloudIdentity{
                                                        Username = "MyUserName", 
                                                        Password = "MyPassword", 
                                                        CloudInstance =CloudInstance.UK});
}
catch(ResponseException ex2)
{
    // do something
}

User Details and Roles

retrieving details about a user is reserved for either admins, or the requesting user retrieving their own information. The examples below show a user retrieving their own if, but we will have an examples of what administrative accounts can do.

Basic user methods

Retrieve user details by username

try
{
    IIdentityProvider identityProvider = new CloudIdentityProvider();
    var identity = new RackspaceCloudIdentity{ Username = "MyUserName",   APIKey = "MyPassword" };
    var userDetails = identityProvider.GetUserByName(identity, "MyUserName");
}
catch(ResponseException ex2)
{
    // do something
}

Retrieve user details by user id

try
{
    IIdentityProvider identityProvider = new CloudIdentityProvider();
    var identity = new RackspaceCloudIdentity{ Username = "MyUserName",   APIKey = "MyPassword" };
    var userDetails = identityProvider.GetUser(identity, "UserId");
}
catch(ResponseException ex2)
{
    // do something
}

Administrative user methods

These methods are only available to user that exist in the identity:user-admin role. If the requesting user does not exist in this role, a exception will be thrown.

Add User

List all global Roles

try
{
    IIdentityProvider identityProvider = new CloudIdentityProvider();
    var identity = new RackspaceCloudIdentity{ Username = "MyAdminUserName",   APIKey = "MyAdminPassword" };
    var userDetails = identityProvider.ListRoles(identity)
}
catch(ResponseException ex2)
{
    // do something
}