/// <summary> /// Validates the specified <see cref="AccessTokenResult"/> object and throws an exception /// in case it is invalid. /// </summary> /// <param name="serviceRoot">The server root <see cref="Uri"/>.</param> /// <param name="accessToken">The <see cref="AccessTokenResult"/> object to be validated.</param> internal static void ValidateAccessToken(Uri serviceRoot, AccessTokenResult accessToken) { if (accessToken == null) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, Resources.InvalidAuthentication, serviceRoot.AbsoluteUri.ToString())); } }
/// <summary> /// Validates the specified <see cref="AccessTokenResult"/> object and throws an exception /// in case it is invalid. /// </summary> /// <param name="serviceRoot">The server root <see cref="Uri"/>.</param> /// <param name="accessToken">The <see cref="AccessTokenResult"/> object to be validated.</param> internal static void ValidateAccessToken(Uri serviceRoot, AccessTokenResult accessToken) { if (accessToken == null) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, Resources.InvalidAuthentication, serviceRoot.AbsoluteUri.ToString())); } }
/// <summary> /// Retrieves and returns the Management Service access token for the specified user credentials. /// </summary> /// <param name="accessUri">The <see cref="Uri"/> to the Management Service <c>GetAccessToken</c> operation.</param> /// <param name="credentials">The credentials to be used to authenticate the user.</param> /// <returns>An instance of <see cref="AccessTokenResult"/> with the retrieved access token and cookie.</returns> public static AccessTokenResult GetAccessToken(Uri accessUri, SqlAuthenticationCredentials credentials) { if (accessUri == null) { throw new ArgumentNullException("accessUri"); } if (credentials == null) { throw new ArgumentNullException("credentials"); } HttpWebRequest request = CreateGetAccessTokenRequest(accessUri, credentials); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); AccessTokenResult result = RetrieveAccessTokenFromResponse(response); return(result); }
/// <summary> /// Retrieves and returns the access token from the specified response or returns <c>null</c> /// if was not retrieved. /// </summary> /// <param name="response">The <see cref="HttpWebResponse"/> to the Management Service <c>GetAccessToken</c> operation.</param> /// <returns>An instance of <see cref="AccessTokenResult"/> with the retrieved access token and cookie.</returns> private static AccessTokenResult RetrieveAccessTokenFromResponse(HttpWebResponse response) { AccessTokenResult result = null; // Read the response into a stream Stream dataStream = response.GetResponseStream(); if (dataStream != null) { string tokenXml; using (StreamReader streamReader = new StreamReader(dataStream, Encoding.UTF8)) { tokenXml = streamReader.ReadToEnd(); } // Must have both a token and cookie for success string accessToken = XElement.Parse(tokenXml).Value; Cookie accessCookie = response.Cookies[DataServiceConstants.AccessCookie]; result = new AccessTokenResult(accessToken, accessCookie); } return result; }
/// <summary> /// Retrieves and returns the access token from the specified response or returns <c>null</c> /// if was not retrieved. /// </summary> /// <param name="response">The <see cref="HttpWebResponse"/> to the Management Service <c>GetAccessToken</c> operation.</param> /// <returns>An instance of <see cref="AccessTokenResult"/> with the retrieved access token and cookie.</returns> private static AccessTokenResult RetrieveAccessTokenFromResponse(HttpWebResponse response) { AccessTokenResult result = null; // Read the response into a stream Stream dataStream = response.GetResponseStream(); if (dataStream != null) { string tokenXml; using (StreamReader streamReader = new StreamReader(dataStream, Encoding.UTF8)) { tokenXml = streamReader.ReadToEnd(); } // Must have both a token and cookie for success string accessToken = XElement.Parse(tokenXml).Value; Cookie accessCookie = response.Cookies[DataServiceConstants.AccessCookie]; result = new AccessTokenResult(accessToken, accessCookie); } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="ServerDataServiceSqlAuth"/> class. /// </summary> /// <param name="managementServiceUri">The server's management service Uri.</param> /// <param name="connectionType">The server connection type with the server name</param> /// <param name="sessionActivityId">An activity ID provided by the user that should be associated with this session.</param> /// <param name="accessToken">The authentication access token to be used for executing web requests.</param> /// <param name="credentials">The SQL authentication credentials used for this context</param> private ServerDataServiceSqlAuth( Uri managementServiceUri, DataServiceConnectionType connectionType, Guid sessionActivityId, AccessTokenResult accessToken, SqlAuthenticationCredentials credentials) : base(new Uri(managementServiceUri, connectionType.RelativeEntityUri)) { this.sessionActivityId = sessionActivityId; this.connectionType = connectionType; this.accessToken = accessToken; this.credentials = credentials; // Generate a requestId and retrieve the server name this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId(); this.serverName = this.Servers.First().Name; }
/// <summary> /// Creates and returns a new instance of the <see cref="ServerDataServiceSqlAuth"/> class which /// connects to the specified server using the specified credentials. /// </summary> /// <param name="managementServiceUri">The server's management service <see cref="Uri"/>.</param> /// <param name="sessionActivityId">An activity ID provided by the user that should be associated with this session.</param> /// <param name="accessTokenResult">The accessToken to be used to authenticate the user.</param> /// <param name="serverName">The name of the server to connect to. (Optional)</param> /// <param name="credentials">The SQL authentication credentials used for this context</param> /// <returns>An instance of <see cref="ServerDataServiceSqlAuth"/> class.</returns> public static ServerDataServiceSqlAuth Create( Uri managementServiceUri, Guid sessionActivityId, AccessTokenResult accessTokenResult, string serverName, SqlAuthenticationCredentials credentials) { if (managementServiceUri == null) { throw new ArgumentNullException("managementServiceUri"); } if (accessTokenResult == null) { throw new ArgumentNullException("accessTokenResult"); } // Create a ServerDataServiceSqlAuth object if (serverName == null) { return new ServerDataServiceSqlAuth( managementServiceUri, new DataServiceConnectionType(ServerModelConnectionType), sessionActivityId, accessTokenResult, credentials); } else { return new ServerDataServiceSqlAuth( managementServiceUri, new DataServiceConnectionType(ServerModelConnectionType, serverName), sessionActivityId, accessTokenResult, credentials); } }