/// <summary> /// Registers the given device with Live ID /// </summary> /// <param name="applicationId">ID for the application</param> /// <param name="issuerUri">URL for the current token issuer</param> /// <param name="deviceName">Device name that should be registered</param> /// <param name="devicePassword">Device password that should be registered</param> /// <returns>ClientCredentials that were registered</returns> /// <remarks> /// The issuerUri can be retrieved from the IServiceConfiguration interface's CurrentIssuer property. /// </remarks> public static ClientCredentials RegisterDevice(Guid applicationId, Uri issuerUri, string deviceName, string devicePassword) { if (string.IsNullOrEmpty(deviceName) && !PersistToFile) { throw new ArgumentNullException("deviceName", "If PersistToFile is false, then deviceName must be specified."); } else if (string.IsNullOrEmpty(deviceName) != string.IsNullOrEmpty(devicePassword)) { throw new ArgumentNullException("deviceName", "Either deviceName/devicePassword should both be specified or they should be null."); } DeviceUserName userNameCredentials; if (string.IsNullOrEmpty(deviceName)) { userNameCredentials = GenerateDeviceUserName(); } else { userNameCredentials = new DeviceUserName() { DeviceName = deviceName, DecryptedPassword = devicePassword }; } return(RegisterDevice(applicationId, issuerUri, userNameCredentials)); }
private static DeviceUserName GenerateDeviceUserName() { DeviceUserName userName = new DeviceUserName(); userName.DeviceName = GenerateRandomString(LiveIdConstants.ValidDeviceNameCharacters, MaxDeviceNameLength); userName.DecryptedPassword = GenerateRandomString(LiveIdConstants.ValidDevicePasswordCharacters, MaxDevicePasswordLength); return(userName); }
private static LiveDevice GenerateDevice(string deviceName, string devicePassword) { // If the deviceName hasn't been specified, it should be generated using random characters. DeviceUserName userNameCredentials; if (string.IsNullOrEmpty(deviceName)) { userNameCredentials = GenerateDeviceUserName(); } else { userNameCredentials = new DeviceUserName() { DeviceName = deviceName, DecryptedPassword = devicePassword }; } return(new LiveDevice() { User = userNameCredentials, Version = 1 }); }
private static ClientCredentials RegisterDevice(Guid applicationId, Uri issuerUri, DeviceUserName userName) { string environment = DiscoverEnvironment(issuerUri); LiveDevice device = new LiveDevice() { User = userName, Version = 1 }; DeviceRegistrationRequest request = new DeviceRegistrationRequest(applicationId, device); string url = string.Format(CultureInfo.InvariantCulture, LiveIdConstants.RegistrationEndpointUriFormat, string.IsNullOrEmpty(environment) ? null : "-" + environment); DeviceRegistrationResponse response = ExecuteRegistrationRequest(url, request); if (!response.IsSuccess) { //If the file is not persisted, the registration will always occur (since the credentials are not //persisted to the disk. However, the credentials may already exist. To avoid an exception being continually //processed by the calling user, DeviceAlreadyExists will be ignored if the credentials are not persisted to the disk. if (!PersistToFile && DeviceRegistrationErrorCode.DeviceAlreadyExists == response.Error.RegistrationErrorCode) { return(device.User.ToClientCredentials()); } throw new DeviceRegistrationFailedException(response.Error.RegistrationErrorCode, response.ErrorSubCode); } if (PersistToFile) { WriteDevice(environment, device); } return(device.User.ToClientCredentials()); }