Ejemplo n.º 1
0
        async public Task <IEnumerable <ActiveDirectoryUser> > ActiveDirectoryUsersAsync()
        {
            List <ActiveDirectoryUser> activeDirectoryUsers = new List <ActiveDirectoryUser>();
            ActiveDirectoryClient      client          = Helpers.AuthenticationHelper.GetActiveDirectoryClient();
            IPagedCollection <IUser>   pagedCollection = await client.Users.ExecuteAsync();

            if (pagedCollection != null)
            {
                do
                {
                    List <IUser> usersList = pagedCollection.CurrentPage.ToList();
                    foreach (IUser user in usersList)
                    {
                        ActiveDirectoryUser adUser = new Models.ActiveDirectoryUser();
                        adUser.ActiveDirectoryId = user.ObjectId;
                        adUser.FullName          = user.DisplayName;
                        adUser.Position          = user.JobTitle;
                        adUser.Location          = user.City + ", " + user.State;
                        adUser.ImageUrl          = "/Users/ShowThumbnail/" + user.ObjectId;
                        adUser.ObjectId          = user.ObjectId;
                        activeDirectoryUsers.Add(adUser);
                    }
                    pagedCollection = await pagedCollection.GetNextPageAsync();
                } while (pagedCollection != null);
            }

            return(activeDirectoryUsers);
        }
Ejemplo n.º 2
0
        // POST: odata/ActiveDirectoryUsers
        public async Task <IHttpActionResult> Post(Models.ActiveDirectoryUser ActiveDirectoryUser)
        {
            string fn = string.Format("{0}:{1}.{2}", this.GetType().Namespace, this.ControllerContext.ControllerDescriptor.ControllerName, this.ControllerContext.Request.GetActionDescriptor().ActionName);

            Debug.WriteLine(fn);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var  ctx             = ServerApplicationContext.Current;
            bool fContextCreated = false;

            try
            {
                bool fReturn = fReturn = false;
                if (null == ctx)
                {
                    fContextCreated = true;
                    ctx             = ServerApplicationContext.CreateContext();
                }
                string permissionId = string.Format("{0}:{1}{2}", "LightSwitchApplication", "ActiveDirectoryUser", "CanCreate");
                fReturn = ctx.Application.User.HasPermission(permissionId);
                if (!fReturn)
                {
                    return(StatusCode(HttpStatusCode.Unauthorized));
                }
                return(StatusCode(HttpStatusCode.NotImplemented));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(String.Format("{0}: {1}\r\n{2}", ex.Source, ex.Message, ex.StackTrace));
                throw;
            }
            finally
            {
                if (fContextCreated && (null != ctx))
                {
                    ctx.Dispose();
                }
            }
        }
Ejemplo n.º 3
0
        private Models.ActiveDirectoryUser ExtractActiveDirectoryUserProperties(SearchResult user)
        {
            if (null == user)
            {
                throw new ArgumentNullException("user", "user: Parameter validation FAILED. SearchResult 'user' must not be 'null'.");
            }

            var entity = new Models.ActiveDirectoryUser();

            foreach (System.Collections.DictionaryEntry property in user.Properties)
            {
                var propInfo = entity.GetType().GetProperty(property.Key.ToString(), BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
                if (null == propInfo)
                {
                    continue;
                }
                var v = property.Value as ResultPropertyValueCollection;
                if (v[0] is Byte[])
                {
                    bool fResult = fResult = false;
                    var  ab      = v[0] as Byte[];
                    if (Guid.Empty.ToByteArray().Length == ab.Length)
                    {
                        var guid = new Guid(ab);
                        propInfo.SetValue(entity, guid.ToString());
                    }
                    else
                    {
                        var sid = new System.Security.Principal.SecurityIdentifier(ab, 0);
                        propInfo.SetValue(entity, sid.ToString());
                    }
                }
                else
                {
                    propInfo.SetValue(entity, v[0]);
                }
            }
            return(entity);
        }