public async Task <ActionResult> About() { var apiBaseUrl = Environment.GetEnvironmentVariable("API_URL") ?? "http://localhost:60201/"; var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }); client.BaseAddress = new Uri(apiBaseUrl); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); string json = await Task.Run(() => JsonConvert.SerializeObject(new UPNInfo() { UPN = LDAPHelper.GetUPN(User.Identity.Name) })); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await client.PostAsync("api/values", content); AdInfo adinfo = new AdInfo(); if (response.IsSuccessStatusCode) { string data = await response.Content.ReadAsStringAsync(); JavaScriptSerializer JSserializer = new JavaScriptSerializer(); adinfo = JSserializer.Deserialize <AdInfo>(data); } return(View(adinfo)); }
public Object Post(UPNInfo upn) { var windowsIdentity = User.Identity as WindowsIdentity; if (windowsIdentity is null) { return("not using windows auth."); } var testData = new List <Result>(); Ldap ldapInfo = new Ldap(); try { PrincipalContext ctx = new PrincipalContext(ContextType.Domain); ldapInfo.ConnectedServer = ctx.ConnectedServer; ldapInfo.Container = ctx.Container; var identity = UserPrincipal.FindByIdentity(ctx, windowsIdentity.Name); ldapInfo.UserPrincipalName = identity?.UserPrincipalName; var connectionString = Environment.GetEnvironmentVariable("CONNECTION") ?? "server=sqlserver.win.local;DataBase=testdb;integrated security=SSPI"; testData = SQLHelper.GetTestData(connectionString, LDAPHelper.GetUPN(upn.UPN)); } catch (Exception ex) { ldapInfo.ErrorMessage = ex.ToString(); } return(new AdInfo() { MachineName = Environment.MachineName, AuthenticationType = User.Identity.AuthenticationType.ToString(), ImpersonationLevel = windowsIdentity.ImpersonationLevel.ToString(), TestData = testData, Claims = windowsIdentity.Claims.DistinctBy(claim => claim.Type).ToDictionary(claim => claim.Type, claim => claim.Value), Groups = windowsIdentity.Groups.Select(x => new AdGroup() { Name = AdGroup.ToName(x), Value = x.Value }).ToList(), LDAP = ldapInfo, }); }
static void AttemptImpersonateUser(string user, Action action) { //still need to get run time version to work. string upn = LDAPHelper.GetUPN(user); if (string.IsNullOrEmpty(upn)) { //unable to find the user. Console.WriteLine(String.Format("Unable to find user: {0} in domain.", user)); action(); Console.WriteLine("Ran action without impersonation"); } else { using (System.Security.Principal.WindowsImpersonationContext impersonationContext = new WindowsIdentity(upn).Impersonate()) { Console.WriteLine("Impersonating user: "******"Ran action with impersonation"); } } }
public Message ReceiveMessage(string qname, string directFormatProtocol) { Message msg = null; try { string upn = LDAPHelper.GetUPN(User); string directFormatName = GetDirectFormatName(qname, directFormatProtocol); if (string.IsNullOrEmpty(upn)) { //unable to find the user. Console.WriteLine("Unable to find user in domain, trying out a regular path instead"); MessageQueue mq = GetMessageQueue(qname, accessMode: QueueAccessMode.PeekAndAdmin); Console.WriteLine(GetQueueMetadata(qname)); mq.Close(); mq.Dispose(); //Release usage of queue //Does this need to use direct queue name? Console.WriteLine(String.Format("Receive with Direct Format Queue Name: {0}", directFormatName)); mq = GetMessageQueue(directFormatName, accessMode: QueueAccessMode.Receive); msg = mq.Receive(); long len = msg.BodyStream.Length; byte[] msgBodyBytes = new byte[(Constants.MAX_MESSAGE_SIZE < (int)len) ? Constants.MAX_MESSAGE_SIZE : (int)len]; if (len > Constants.MAX_MESSAGE_SIZE) { //do something here? Console.WriteLine("Message contents larger than expected, possible truncation."); } msg.BodyStream.Read(msgBodyBytes, 0, (int)len); //repack the message contents into the body of the message. msg.Body = Encoding.ASCII.GetString(msgBodyBytes); mq.Close(); mq.Dispose(); } else { using (System.Security.Principal.WindowsImpersonationContext impersonationContext = new WindowsIdentity(upn).Impersonate()) { MessageQueue mq = GetMessageQueue(qname, accessMode: QueueAccessMode.PeekAndAdmin); Console.WriteLine(GetQueueMetadata(qname)); mq.Close(); mq.Dispose(); //Does this need to use direct queue name? Console.WriteLine(String.Format("Receive with Direct Format Queue Name: {0}", directFormatName)); mq = GetMessageQueue(directFormatName); msg = mq.Receive(); long len = msg.BodyStream.Length; byte[] msgBodyBytes = new byte[(Constants.MAX_MESSAGE_SIZE < (int)len) ? Constants.MAX_MESSAGE_SIZE : (int)len]; if (len > Constants.MAX_MESSAGE_SIZE) { //do something here? Console.WriteLine("Message contents larger than expected, possible truncation."); } msg.BodyStream.Read(msgBodyBytes, 0, (int)len); //repack the message contents into the body of the message. msg.Body = Encoding.ASCII.GetString(msgBodyBytes); mq.Close(); mq.Dispose(); } } } catch (Exception ex) { Console.WriteLine("issue with receiving message."); if (Trace >= TraceLevel.Info) { Console.WriteLine(ex.ToString()); } } return(msg); }
public void SendMessage(string qname, string directFormatProtocol, string messageBody, string label) { CreateQueue(qname); try { string upn = LDAPHelper.GetUPN(User); string directFormatName = GetDirectFormatName(qname, directFormatProtocol); if (string.IsNullOrEmpty(upn)) { //unable to find the user. Console.WriteLine("Unable to find User in domain, trying out a regular path instead"); Message msg = new Message(); int len = messageBody.Length; if (len > Constants.MAX_MESSAGE_SIZE) { //do something here? Console.WriteLine("Message contents larger than expected, possible truncation."); } msg.BodyStream = new MemoryStream(Encoding.ASCII.GetBytes(messageBody)); msg.Label = label; msg.UseDeadLetterQueue = true; //Log metadata with both qname LogQueueMetadata(qname); //Log Metadata with direct format name (permissions issue?) // LogQueueMetadata(directFormatName); //send with direct format name Console.WriteLine(String.Format("Send with Direct Format Queue Name: {0}", directFormatName)); MessageQueue mq = GetMessageQueue(directFormatName, accessMode: QueueAccessMode.Send); mq.Send(msg, MessageQueueTransactionType.Single); mq.Close(); mq.Dispose(); } else { using (System.Security.Principal.WindowsImpersonationContext impersonationContext = new WindowsIdentity(upn).Impersonate()) { Message msg = new Message(); int len = messageBody.Length; if (len > Constants.MAX_MESSAGE_SIZE) { //do something here? Console.WriteLine("Message contents larger than expected, possible truncation."); } msg.BodyStream = new MemoryStream(Encoding.ASCII.GetBytes(messageBody)); msg.Label = label; msg.UseDeadLetterQueue = true; //Log metadata with both qname LogQueueMetadata(qname); //Log Metadata with direct format name (permissions issue?) // LogQueueMetadata(directFormatName); //send with direct format name Console.WriteLine(String.Format("Send with Direct Format Queue Name: {0}", directFormatName)); MessageQueue mq = GetMessageQueue(directFormatName, accessMode: QueueAccessMode.Send); mq.Send(msg, MessageQueueTransactionType.Single); mq.Close(); mq.Dispose(); } } } catch (Exception ex) { Console.WriteLine("issue with sending message."); if (Trace >= TraceLevel.Info) { Console.WriteLine(ex.ToString()); } } }
void CreateQueue(string qname, bool transactional = true) { Console.WriteLine("Testing with Queue Name: " + qname); try { string upn = LDAPHelper.GetUPN(User); if (string.IsNullOrEmpty(upn)) { //unable to find the user. Console.WriteLine("Unable to find user in domain, trying out a regular path instead"); StringBuilder sb = new StringBuilder(); sb.AppendLine("Computer Name: " + Environment.MachineName); sb.AppendLine("Logged in User: "******"Registry Key Value: " + RegistryHelper.GetRegistryValue(Constants.REGISTRY_HKLM, Constants.REGISTRY_MSMQ_PARAMETERS, Constants.REGISTRY_MSMQ_WORKGROUP)); Console.WriteLine(sb.ToString()); Console.WriteLine("Checking for queue existance " + qname); if (!MessageQueue.Exists(qname)) { Console.WriteLine("Queue doesn't exist so we will create one."); MessageQueue mq = MessageQueue.Create(qname, transactional); //This should only be set for containers. Otherwise we can use the Current User WindowsIdentity.GetCurrent().Name mq.SetPermissions(Constants.EVERYONE, MessageQueueAccessRights.FullControl); Console.WriteLine("Setting Permissions"); mq.SetPermissions(Constants.AUTHENTICATED_USERS, MessageQueueAccessRights.FullControl); Console.WriteLine("Finished Setting Permissions"); } Console.WriteLine("Queue should exist! " + qname); Console.WriteLine("Ran action without impersonation"); } else { using (System.Security.Principal.WindowsImpersonationContext impersonationContext = new WindowsIdentity(upn).Impersonate()) { Console.WriteLine("Impersonating user: "******"Computer Name: " + Environment.MachineName); sb.AppendLine("Logged in User: "******"Registry Key Value: " + RegistryHelper.GetRegistryValue(Constants.REGISTRY_HKLM, Constants.REGISTRY_MSMQ_PARAMETERS, Constants.REGISTRY_MSMQ_WORKGROUP)); Console.WriteLine(sb.ToString()); Console.WriteLine("Checking for queue existance " + qname); if (!MessageQueue.Exists(qname)) { Console.WriteLine("Queue doesn't exist so we will create one."); MessageQueue mq = MessageQueue.Create(qname, transactional); //This should only be set for containers. Otherwise we can use the Current User WindowsIdentity.GetCurrent().Name mq.SetPermissions(Constants.EVERYONE, MessageQueueAccessRights.FullControl); Console.WriteLine("Setting Permissions"); mq.SetPermissions(Constants.AUTHENTICATED_USERS, MessageQueueAccessRights.FullControl); Console.WriteLine("Finished Setting Permissions"); } Console.WriteLine("Queue should exist! " + qname); Console.WriteLine("Ran action with impersonation"); } } } catch (Exception ex) { Console.WriteLine("issue with creating the queue."); if (Trace >= TraceLevel.Info) { Console.WriteLine(ex.ToString()); } } }