public DeviceRegistrationRequest(Guid applicationId, LiveDevice device)
            : this()
        {
            if (null == device)
            {
                throw new ArgumentNullException("device");
            }

            this.ClientInfo = new DeviceRegistrationClientInfo()
            {
                ApplicationId = applicationId, Version = "1.0"
            };
            this.Authentication = new DeviceRegistrationAuthentication()
            {
                MemberName = device.User.DeviceId,
                Password   = device.User.DecryptedPassword
            };
        }
Пример #2
0
        /// <summary>
        /// Loads the device's credentials from the file system
        /// </summary>
        /// <param name="issuerUri">URL for the current token issuer</param>
        /// <returns>Device Credentials (if set) or null</returns>
        /// <remarks>
        /// The issuerUri can be retrieved from the IServiceConfiguration interface's CurrentIssuer property.
        /// </remarks>
        public static ClientCredentials LoadDeviceCredentials(Uri issuerUri)
        {
            //If the credentials should not be persisted to a file, then they won't be present on the disk.
            if (!PersistToFile)
            {
                return(null);
            }

            EnvironmentConfiguration environment = DiscoverEnvironmentInternal(issuerUri);

            LiveDevice device = ReadExistingDevice(environment);

            if (null == device || null == device.User)
            {
                return(null);
            }

            return(device.User.ToClientCredentials());
        }
Пример #3
0
 public DeviceRegistrationRequest(Guid applicationId, LiveDevice device)
     : this()
 {
     if (device == null)
     {
         throw new ArgumentNullException("device");
     }
     var info = new DeviceRegistrationClientInfo
         {
             ApplicationId = applicationId,
             Version = "1.0"
         };
     ClientInfo = info;
     var authentication = new DeviceRegistrationAuthentication
         {
             MemberName = device.User.DeviceId,
             Password = device.User.DecryptedPassword
         };
     Authentication = authentication;
 }
Пример #4
0
        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());
        }
Пример #5
0
        private static ClientCredentials RegisterDevice(Guid applicationId, Uri issuerUri, LiveDevice device)
        {
            EnvironmentConfiguration environment = DiscoverEnvironmentInternal(issuerUri);

            DeviceRegistrationRequest request = new DeviceRegistrationRequest(applicationId, device);

            string url = string.Format(CultureInfo.InvariantCulture, LiveIdConstants.RegistrationEndpointUriFormat,
                                       environment.HostName);

            DeviceRegistrationResponse response = ExecuteRegistrationRequest(url, request);

            if (!response.IsSuccess)
            {
                bool throwException = true;
                if (DeviceRegistrationErrorCode.DeviceAlreadyExists == response.Error.RegistrationErrorCode)
                {
                    if (!PersistToFile)
                    {
                        //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.
                        return(device.User.ToClientCredentials());
                    }
                    else if (PersistIfDeviceAlreadyExists)
                    {
                        // This flag indicates that the
                        throwException = false;
                    }
                }

                if (throwException)
                {
                    throw new DeviceRegistrationFailedException(response.Error.RegistrationErrorCode, response.ErrorSubCode);
                }
            }

            if (PersistToFile || PersistIfDeviceAlreadyExists)
            {
                WriteDevice(environment, device);
            }

            return(device.User.ToClientCredentials());
        }