public static PSADObject ToPSADGroup(this AADObject obj) { return(new PSADObject() { DisplayName = obj.DisplayName, Id = obj.ObjectId }); }
public static PSADObject ToPSADGroup(this AADObject obj) { return new PSADObject() { DisplayName = obj.DisplayName, Id = new Guid(obj.ObjectId) }; }
public static PSADObject ToPSADGroup(this AADObject obj) { var adObj = new PSADObject() { DisplayName = obj.DisplayName, }; return(AssignObjectId(adObj, obj.ObjectId)); }
public static PSADObject ToPSADObject(this AADObject obj) { if (obj == null) { throw new ArgumentNullException(); } if (obj.ObjectType == typeof(User).Name) { var adUser = new PSADUser() { DisplayName = obj.DisplayName, Type = obj.ObjectType, UserPrincipalName = obj.UserPrincipalName }; return(AssignObjectId(adUser, obj.ObjectId)); } else if (obj.ObjectType == "Group") { var adGroup = new PSADGroup() { DisplayName = obj.DisplayName, Type = obj.ObjectType, SecurityEnabled = obj.SecurityEnabled, MailNickname = obj.Mail }; return(AssignObjectId(adGroup, obj.ObjectId)); } else if (obj.ObjectType == typeof(ServicePrincipal).Name) { var adSp = new PSADServicePrincipal() { DisplayName = obj.DisplayName, Type = obj.ObjectType, ServicePrincipalNames = obj.ServicePrincipalNames.ToArray() }; return(AssignObjectId(adSp, obj.ObjectId)); } else { var adObj = new PSADObject() { DisplayName = obj.DisplayName, Type = obj.ObjectType }; return(AssignObjectId(adObj, obj.ObjectId)); } }
public static PSADObject ToPSADObject(this AADObject obj) { if (obj == null) { throw new ArgumentNullException(); } if (obj.ObjectType == typeof(User).Name) { return(new PSADUser() { DisplayName = obj.DisplayName, Id = obj.ObjectId, Type = obj.ObjectType, UserPrincipalName = obj.UserPrincipalName }); } else if (obj.ObjectType == "Group") { return(new PSADGroup() { DisplayName = obj.DisplayName, Id = obj.ObjectId, Type = obj.ObjectType, SecurityEnabled = obj.SecurityEnabled, MailNickname = !string.IsNullOrEmpty(obj.Mail) ? obj.Mail : !string.IsNullOrEmpty(obj.MailNickname) ? obj.MailNickname : obj.AdditionalProperties.ContainsKey("mailNickname") ? obj.AdditionalProperties["mailNickname"]?.ToString() : null, Description = obj.AdditionalProperties.ContainsKey("description") ? obj.AdditionalProperties["description"]?.ToString() : null }); } else if (obj.ObjectType == typeof(ServicePrincipal).Name) { return(new PSADServicePrincipal() { DisplayName = obj.DisplayName, Id = obj.ObjectId, Type = obj.ObjectType, ServicePrincipalNames = obj.ServicePrincipalNames.ToArray() }); } else { return(new PSADObject() { DisplayName = obj.DisplayName, Id = obj.ObjectId, Type = obj.ObjectType }); } }
public static PSADObject ToPSADObject(this AADObject obj) { if (obj == null) { throw new ArgumentNullException(); } if (obj.ObjectType == typeof(User).Name) { return(new PSADUser() { DisplayName = obj.DisplayName, Id = new Guid(obj.ObjectId), Type = obj.ObjectType, UserPrincipalName = obj.UserPrincipalName, SignInName = obj.SignInName, Mail = obj.Mail }); } else if (obj.ObjectType == typeof(Group).Name) { return(new PSADGroup() { DisplayName = obj.DisplayName, Type = obj.ObjectType, Id = new Guid(obj.ObjectId), SecurityEnabled = obj.SecurityEnabled/*, * Mail = group.Mail*/ }); } else if (obj.ObjectType == typeof(ServicePrincipal).Name) { return(new PSADServicePrincipal() { DisplayName = obj.DisplayName, Id = new Guid(obj.ObjectId), Type = obj.ObjectType, ServicePrincipalName = obj.ServicePrincipalNames.FirstOrDefault() }); } else { return(new PSADObject() { DisplayName = obj.DisplayName, Id = new Guid(obj.ObjectId), Type = obj.ObjectType }); } }
public static PSADObject ToPSADObject(this AADObject obj) { if (obj == null) { throw new ArgumentNullException(); } if (obj.ObjectType == typeof(User).Name) { return(new PSADUser() { DisplayName = obj.DisplayName, Id = new Guid(obj.ObjectId), Type = obj.ObjectType, UserPrincipalName = obj.UserPrincipalName }); } else if (obj.ObjectType == "Group") { return(new PSADGroup() { DisplayName = obj.DisplayName, Type = obj.ObjectType, Id = new Guid(obj.ObjectId), SecurityEnabled = obj.SecurityEnabled, MailNickname = obj.Mail }); } else if (obj.ObjectType == typeof(ServicePrincipal).Name) { return(new PSADServicePrincipal() { DisplayName = obj.DisplayName, Id = new Guid(obj.ObjectId), Type = obj.ObjectType, ServicePrincipalNames = obj.ServicePrincipalNames.ToArray() }); } else { return(new PSADObject() { DisplayName = obj.DisplayName, Id = new Guid(obj.ObjectId), Type = obj.ObjectType }); } }
public PSADApplication CreateApplication(CreatePSApplicationParameters createParameters) { IList <PasswordCredential> passwordCredentials = createParameters.PasswordCredentials != null ? createParameters.PasswordCredentials.Select(psCredential => psCredential.ToGraphPasswordCredential()).ToList() : null; IList <KeyCredential> keyCredentials = createParameters.KeyCredentials != null ? createParameters.KeyCredentials.Select(psCredential => psCredential.ToGraphKeyCredential()).ToList() : null; ApplicationCreateParameters graphParameters = new ApplicationCreateParameters { DisplayName = createParameters.DisplayName, Homepage = createParameters.HomePage, IdentifierUris = createParameters.IdentifierUris, ReplyUrls = createParameters.ReplyUrls, AvailableToOtherTenants = createParameters.AvailableToOtherTenants, PasswordCredentials = passwordCredentials, KeyCredentials = keyCredentials }; try { return(GraphClient.Applications.Create(graphParameters).ToPSADApplication()); } catch (GraphErrorException ce) { if (ce.Response.StatusCode == HttpStatusCode.Forbidden) { AADObject currentUser = GraphClient.Objects.GetCurrentUser(); if (currentUser != null && string.Equals(currentUser.UserType, "Guest", StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException(ProjectResources.CreateApplicationNotAllowedGuestUser); } } throw; } }
public static PSADObject ToPSADObject(this AADObject obj) { if (obj == null) { throw new ArgumentNullException(); } if (obj.ObjectType == typeof(User).Name) { return(new PSADUser() { DisplayName = obj.DisplayName, Id = new Guid(obj.ObjectId), Type = obj.ObjectType, UserPrincipalName = obj.UserPrincipalName, Mail = obj.Mail }); } else if (obj.ObjectType == typeof(Group).Name) { return(new PSADGroup() { DisplayName = obj.DisplayName, Type = obj.ObjectType, Id = new Guid(obj.ObjectId)/*, * Mail = group.Mail*/ }); } else { return(new PSADObject() { DisplayName = obj.DisplayName, Id = new Guid(obj.ObjectId), Type = obj.ObjectType }); } }
public PSADServicePrincipal CreateServicePrincipal(CreatePSServicePrincipalParameters createParameters) { IList <PasswordCredential> passwordCredentials = createParameters.PasswordCredentials != null ? createParameters.PasswordCredentials.Select(psCredential => psCredential.ToGraphPasswordCredential()).ToList() : null; IList <KeyCredential> keyCredentials = createParameters.KeyCredentials != null ? createParameters.KeyCredentials.Select(psCredential => psCredential.ToGraphKeyCredential()).ToList() : null; ServicePrincipalCreateParameters graphParameters = new ServicePrincipalCreateParameters { AppId = createParameters.ApplicationId.ToString(), AccountEnabled = createParameters.AccountEnabled, KeyCredentials = keyCredentials, PasswordCredentials = passwordCredentials }; try { return(GraphClient.ServicePrincipals.Create(graphParameters).ToPSADServicePrincipal()); } catch (GraphErrorException ce) { if (ce.Response.StatusCode == HttpStatusCode.Forbidden) { AADObject currentUser = GraphClient.Objects.GetCurrentUser(); if (currentUser != null && string.Equals(currentUser.UserType, "Guest", StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException(ProjectResources.CreateServicePrincipalNotAllowedGuestUser); } } throw; } }
/// <summary> /// Gets the details for current logged in user /// </summary> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Server response for Active Directory objects inquiry API calls /// </returns> public async Task <GetCurrentUserResult> GetCurrentUserAsync(CancellationToken cancellationToken) { // Validate // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); TracingAdapter.Enter(invocationId, this, "GetCurrentUserAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/"; url = url + Uri.EscapeDataString(this.Client.TenantID); url = url + "/me"; List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=1.6-internal"); if (queryParameters.Count > 0) { url = url + "?" + string.Join("&", queryParameters); } string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Get; httpRequest.RequestUri = new Uri(url); // Set Headers // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result GetCurrentUserResult result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new GetCurrentUserResult(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { AADObject aADObjectInstance = new AADObject(); result.AADObject = aADObjectInstance; JToken objectIdValue = responseDoc["objectId"]; if (objectIdValue != null && objectIdValue.Type != JTokenType.Null) { string objectIdInstance = ((string)objectIdValue); aADObjectInstance.ObjectId = objectIdInstance; } JToken objectTypeValue = responseDoc["objectType"]; if (objectTypeValue != null && objectTypeValue.Type != JTokenType.Null) { string objectTypeInstance = ((string)objectTypeValue); aADObjectInstance.ObjectType = objectTypeInstance; } JToken displayNameValue = responseDoc["displayName"]; if (displayNameValue != null && displayNameValue.Type != JTokenType.Null) { string displayNameInstance = ((string)displayNameValue); aADObjectInstance.DisplayName = displayNameInstance; } JToken userPrincipalNameValue = responseDoc["userPrincipalName"]; if (userPrincipalNameValue != null && userPrincipalNameValue.Type != JTokenType.Null) { string userPrincipalNameInstance = ((string)userPrincipalNameValue); aADObjectInstance.UserPrincipalName = userPrincipalNameInstance; } JToken mailValue = responseDoc["mail"]; if (mailValue != null && mailValue.Type != JTokenType.Null) { string mailInstance = ((string)mailValue); aADObjectInstance.Mail = mailInstance; } JToken mailEnabledValue = responseDoc["mailEnabled"]; if (mailEnabledValue != null && mailEnabledValue.Type != JTokenType.Null) { bool mailEnabledInstance = ((bool)mailEnabledValue); aADObjectInstance.MailEnabled = mailEnabledInstance; } JToken securityEnabledValue = responseDoc["securityEnabled"]; if (securityEnabledValue != null && securityEnabledValue.Type != JTokenType.Null) { bool securityEnabledInstance = ((bool)securityEnabledValue); aADObjectInstance.SecurityEnabled = securityEnabledInstance; } JToken signInNameValue = responseDoc["signInName"]; if (signInNameValue != null && signInNameValue.Type != JTokenType.Null) { string signInNameInstance = ((string)signInNameValue); aADObjectInstance.SignInName = signInNameInstance; } JToken servicePrincipalNamesArray = responseDoc["servicePrincipalNames"]; if (servicePrincipalNamesArray != null && servicePrincipalNamesArray.Type != JTokenType.Null) { foreach (JToken servicePrincipalNamesValue in ((JArray)servicePrincipalNamesArray)) { aADObjectInstance.ServicePrincipalNames.Add(((string)servicePrincipalNamesValue)); } } JToken userTypeValue = responseDoc["userType"]; if (userTypeValue != null && userTypeValue.Type != JTokenType.Null) { string userTypeInstance = ((string)userTypeValue); aADObjectInstance.UserType = userTypeInstance; } } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("request-id")) { result.RequestId = httpResponse.Headers.GetValues("request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
/// <summary> /// Gets AD group membership by provided AD object Ids /// </summary> /// <param name='parameters'> /// Required. Objects filtering parameters. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Server response for Active Directory objects inquiry API calls /// </returns> public async Task <GetObjectsResult> GetObjectsByObjectIdsAsync(GetObjectsParameters parameters, CancellationToken cancellationToken) { // Validate if (parameters == null) { throw new ArgumentNullException("parameters"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("parameters", parameters); TracingAdapter.Enter(invocationId, this, "GetObjectsByObjectIdsAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/"; url = url + Uri.EscapeDataString(this.Client.TenantID); url = url + "/getObjectsByObjectIds"; List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=1.6-internal"); if (queryParameters.Count > 0) { url = url + "?" + string.Join("&", queryParameters); } string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Post; httpRequest.RequestUri = new Uri(url); // Set Headers // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; JToken requestDoc = null; JObject getObjectsParametersValue = new JObject(); requestDoc = getObjectsParametersValue; if (parameters.Ids != null) { if (parameters.Ids is ILazyCollection == false || ((ILazyCollection)parameters.Ids).IsInitialized) { JArray objectIdsArray = new JArray(); foreach (string objectIdsItem in parameters.Ids) { objectIdsArray.Add(objectIdsItem); } getObjectsParametersValue["objectIds"] = objectIdsArray; } } if (parameters.Types != null) { if (parameters.Types is ILazyCollection == false || ((ILazyCollection)parameters.Types).IsInitialized) { JArray typesArray = new JArray(); foreach (string typesItem in parameters.Types) { typesArray.Add(typesItem); } getObjectsParametersValue["types"] = typesArray; } } getObjectsParametersValue["includeDirectoryObjectReferences"] = parameters.IncludeDirectoryObjectReferences; requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result GetObjectsResult result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new GetObjectsResult(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { JToken valueArray = responseDoc["value"]; if (valueArray != null && valueArray.Type != JTokenType.Null) { foreach (JToken valueValue in ((JArray)valueArray)) { AADObject aADObjectInstance = new AADObject(); result.AADObject.Add(aADObjectInstance); JToken objectIdValue = valueValue["objectId"]; if (objectIdValue != null && objectIdValue.Type != JTokenType.Null) { string objectIdInstance = ((string)objectIdValue); aADObjectInstance.ObjectId = objectIdInstance; } JToken objectTypeValue = valueValue["objectType"]; if (objectTypeValue != null && objectTypeValue.Type != JTokenType.Null) { string objectTypeInstance = ((string)objectTypeValue); aADObjectInstance.ObjectType = objectTypeInstance; } JToken displayNameValue = valueValue["displayName"]; if (displayNameValue != null && displayNameValue.Type != JTokenType.Null) { string displayNameInstance = ((string)displayNameValue); aADObjectInstance.DisplayName = displayNameInstance; } JToken userPrincipalNameValue = valueValue["userPrincipalName"]; if (userPrincipalNameValue != null && userPrincipalNameValue.Type != JTokenType.Null) { string userPrincipalNameInstance = ((string)userPrincipalNameValue); aADObjectInstance.UserPrincipalName = userPrincipalNameInstance; } JToken mailValue = valueValue["mail"]; if (mailValue != null && mailValue.Type != JTokenType.Null) { string mailInstance = ((string)mailValue); aADObjectInstance.Mail = mailInstance; } JToken mailEnabledValue = valueValue["mailEnabled"]; if (mailEnabledValue != null && mailEnabledValue.Type != JTokenType.Null) { bool mailEnabledInstance = ((bool)mailEnabledValue); aADObjectInstance.MailEnabled = mailEnabledInstance; } JToken securityEnabledValue = valueValue["securityEnabled"]; if (securityEnabledValue != null && securityEnabledValue.Type != JTokenType.Null) { bool securityEnabledInstance = ((bool)securityEnabledValue); aADObjectInstance.SecurityEnabled = securityEnabledInstance; } JToken signInNameValue = valueValue["signInName"]; if (signInNameValue != null && signInNameValue.Type != JTokenType.Null) { string signInNameInstance = ((string)signInNameValue); aADObjectInstance.SignInName = signInNameInstance; } JToken servicePrincipalNamesArray = valueValue["servicePrincipalNames"]; if (servicePrincipalNamesArray != null && servicePrincipalNamesArray.Type != JTokenType.Null) { foreach (JToken servicePrincipalNamesValue in ((JArray)servicePrincipalNamesArray)) { aADObjectInstance.ServicePrincipalNames.Add(((string)servicePrincipalNamesValue)); } } JToken userTypeValue = valueValue["userType"]; if (userTypeValue != null && userTypeValue.Type != JTokenType.Null) { string userTypeInstance = ((string)userTypeValue); aADObjectInstance.UserType = userTypeInstance; } } } JToken odatanextLinkValue = responseDoc["odata.nextLink"]; if (odatanextLinkValue != null && odatanextLinkValue.Type != JTokenType.Null) { string odatanextLinkInstance = ((string)odatanextLinkValue); result.NextLink = odatanextLinkInstance; } } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("request-id")) { result.RequestId = httpResponse.Headers.GetValues("request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }