We have an internal site which was built on C# and has various tools for various functions to help the company like looking up/modifying/adding service accounts, for example. The tools are just C# code to automate these tasks. /vague
I've been asked to find/create a way to enter an SPN and have it return the service account which it's associated to.
So something like:
Input: HTTP/server1.company.com
Output: SVC_ACCT_AWESOME
The other request I got was for entering a service account name and have it give the KVNO back.
Issue, I have never done anything in C# before. I have Visual Studio 2013 loaded and I want to learn. I've spent hours pouring over the Googles and haven't found anything useful other than this:
https://msdn.microsoft.com/en-us/library/vstudio/system.servicemodel.configuration.identityelement.serviceprincipalname(v=vs.100).aspx
Unfortunately it doesn't mean anything to me.
Any direction that can be given for these specific tasks would be super appreciated!
Thanks.
I believe you can query AD for SPNs with something like this:
using (var root = new DirectoryEntry()) // or pass in something like "LDAP://dc=example,dc=com" to query a different domain
using (var searcher = new DirectorySearcher(root))
{
searcher.Filter = "(servicePrincipalName=HTTP/server1.company.com)";
using (var results = searcher.FindAll())
{
foreach (SearchResult result in results)
Console.WriteLine(result.Properties["samAccountName"][0]); // or whatever you want to do with it
}
}
(this needs a reference to System.DirectoryServices)
Related
I need to get the direct reports from a logged in user (MVC 4)
I don't need the names of the direct reports but I do need their email addresses including their proxy addresses.
So for this reason I need to search through Exchange. I personally have never attempted to search Exchange in the past and everything I find out there tells me how to get from step 8 to the finish line but says nothing about how to go from step 1 to 8.
I can get the current users user name by simply
User.Identity.Name.Replace(#"yourdomain\", "")
and I have found this example which so far is probably the best example I have found
http://msdn.microsoft.com/en-us/library/office/ff184617(v=office.15).aspx
but even with that example the line
Outlook.AddressEntry currentUser =
Application.Session.CurrentUser.AddressEntry;
is not actually getting the current user logged into the site.
I really hope someone out there is familiar with this and can get me past this point.
I reworked the sample from the URL as the following LINQPad 4 query. I've found that LINQPad is a great way to experiment because it is very scripty, allowing quick experimentation, and you can easily view data by using the Dump() extension method. Purchasing intellisense support is totally worthwhile.
Also, I noticed there is a lot of fine print like:
The logged-on user must be online for this method to return an AddressEntries collection; otherwise, GetDirectReports returns a null reference. For production code, you must test for the user being offline by using the _NameSpace.ExchangeConnectionMode property, or the _Account.ExchangeConnectionMode property for multiple Exchange scenarios.
and
If the current user has a manager, GetDirectReports() is called to return an AddressEntries collection that represents the address entries for all the direct reports of user’s manager. If the manager has no direct reports, GetDirectReports returns an AddressEntries collection that has a count of zero.
So there are a lot of assumptions like Exchange is configured properly with Direct Report relationships, and the current user is online...which I believe brings Lync into the equation. Hopefully this LINQPad query will be useful to you. Just copy and paste it into a text editor and name it with the .linq file extension. You'll then be able to open it in LINQPad 4. BTW: You're question caught my attention because there was talk recently at my work of pulling direct reports from Active Directory. I wish I could be more helpful...good luck.
<Query Kind="Program">
<Reference><ProgramFilesX86>\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.Outlook.dll</Reference>
<Reference><ProgramFilesX86>\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.OutlookViewCtl.dll</Reference>
<Namespace>Microsoft.Office.Interop.Outlook</Namespace>
</Query>
void Main()
{
GetManagerDirectReports();
}
// Define other methods and classes here
private void GetManagerDirectReports()
{
var app = new Microsoft.Office.Interop.Outlook.Application();
AddressEntry currentUser = app.Session.CurrentUser.AddressEntry;
if (currentUser.Type == "EX")
{
ExchangeUser manager = currentUser.GetExchangeUser().GetExchangeUserManager();
manager.Dump();
if (manager != null)
{
AddressEntries addrEntries = manager.GetDirectReports();
if (addrEntries != null)
{
foreach (AddressEntry addrEntry in addrEntries)
{
ExchangeUser exchUser = addrEntry.GetExchangeUser();
StringBuilder sb = new StringBuilder();
sb.AppendLine("Name: " + exchUser.Name);
sb.AppendLine("Title: " + exchUser.JobTitle);
sb.AppendLine("Department: " + exchUser.Department);
sb.AppendLine("Location: " + exchUser.OfficeLocation);
sb.Dump();
}
}
}
}
}
I would suggest using EWS Managed API in conjunction with your code to get the direct reports for a user. As Jeremy mentioned in his response that you need to have your direct report relationships already set up. To help you get started, here some steps to get EWS Managed API up and running:
Download the latest version of EWS Managed API
Get started with EWS Managed API client applications to learn about how to reference the assembly, set the service URL, and communicate with EWS.
Start working with your code. If you need some functioning code to get you going, check out the Exchange 2013 101 Code Samples that has some authentication code already written and a bunch of examples you can modify to make your own.
If you have the email address or user name of the current user you can use the ResolveName() method to get to their mailbox to retrieve additional information. Here is an article to help with that method: How to: Resolve ambiguous names by using EWS in Exchange 2013
Essentially you want to get to the point where you can run a command similar to this:
NameResolutionCollection coll = service.ResolveName(NameToResolve, ResolveNameSearchLocation.DirectoryOnly, true, new PropertySet(BasePropertySet.FirstClassProperties));
If you give a unique enough value in the NameToResolve parameter you should only get back one item in the collection. With that, you can look at the direct reports collection within that one item and see not only the names of their direct reports, but their email addresses as well.
I hope this information helps. If this does resolve your problem, please mark the post as answered.
Thanks,
--- Bob ---
I was wondering if there is a way to get all the computer names that show up in my network places using C#.
You will want to use the NetServerEnum() API. I dont believe there is a managed wrapper for this in the base .NET libraries but I was able to find this with a quick google search: http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using
NOTE: I haven't tested or thoroughly reviewed the codeproject code but it should be enough of a starting point for what you need if there are any issues.
EDIT: Do not use DirectoryServices unless your sure of a domain environment. The System.DirectoryServices class is an ADSI wrapper that dosent work without an Active Directory to query against. NetServerEnum() works on workgroups and domains but dosen't guarantee the most reliable data (not all machines may show up). It relies on the Computer Browser service.
The best solution would probably be a class that wraps both possibilities and merges the results :/
This works, but it takes a while. :/
public List<String> ListNetworkComputers()
{
List<String> _ComputerNames = new List<String>();
String _ComputerSchema = "Computer";
System.DirectoryServices.DirectoryEntry _WinNTDirectoryEntries = new System.DirectoryServices.DirectoryEntry("WinNT:");
foreach (System.DirectoryServices.DirectoryEntry _AvailDomains in _WinNTDirectoryEntries.Children) {
foreach (System.DirectoryServices.DirectoryEntry _PCNameEntry in _AvailDomains.Children) {
if (_PCNameEntry.SchemaClassName.ToLower().Contains(_ComputerSchema.ToLower())) {
_ComputerNames.Add(_PCNameEntry.Name);
}
}
}
return _ComputerNames;
}
Depends on the user's permission, the application may or may not get those information.
Try using ActiveDirectory. This should get you precise information about the local network.
Use System.DirectoryServices.
I was wondering if there is a way to get all the computer names that show up in my network places using C#.
You will want to use the NetServerEnum() API. I dont believe there is a managed wrapper for this in the base .NET libraries but I was able to find this with a quick google search: http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using
NOTE: I haven't tested or thoroughly reviewed the codeproject code but it should be enough of a starting point for what you need if there are any issues.
EDIT: Do not use DirectoryServices unless your sure of a domain environment. The System.DirectoryServices class is an ADSI wrapper that dosent work without an Active Directory to query against. NetServerEnum() works on workgroups and domains but dosen't guarantee the most reliable data (not all machines may show up). It relies on the Computer Browser service.
The best solution would probably be a class that wraps both possibilities and merges the results :/
This works, but it takes a while. :/
public List<String> ListNetworkComputers()
{
List<String> _ComputerNames = new List<String>();
String _ComputerSchema = "Computer";
System.DirectoryServices.DirectoryEntry _WinNTDirectoryEntries = new System.DirectoryServices.DirectoryEntry("WinNT:");
foreach (System.DirectoryServices.DirectoryEntry _AvailDomains in _WinNTDirectoryEntries.Children) {
foreach (System.DirectoryServices.DirectoryEntry _PCNameEntry in _AvailDomains.Children) {
if (_PCNameEntry.SchemaClassName.ToLower().Contains(_ComputerSchema.ToLower())) {
_ComputerNames.Add(_PCNameEntry.Name);
}
}
}
return _ComputerNames;
}
Depends on the user's permission, the application may or may not get those information.
Try using ActiveDirectory. This should get you precise information about the local network.
Use System.DirectoryServices.
I am not an expert in P4.NET plugin, but I would like to show the existing workspaces for a user in a combo box, so that I can set the p4.Client to the selected workspace.
using (var p4 = new P4Connection())
{
p4.Connect();
???
}
How do I get the list of existing workspaces?
I think the command line to achieve this would be
p4 clients -m 100 -u username
If P4.Net behaves similar to the official Perforce APIs, then you would likely want to run:
p4.Run("clients", "-m 100 -u username")
or similar. Inspired by the P4Ruby documentation.
Ok I have no choice than answering my own question, because the code would be too much to insert as comments to jhwist answer. Sorry jhwist. I had no choice.
#appinger, I hope you find this answer helpful. Took me hours to figure out this api working. :)
cmbBoxPerforceWorkspaceLocation is just your combobox for your workspaces. I am using Winforms by the way.
I need to extract a shortname from the windows username. Windows username starts usually with xxxx\\username. In my code I extract the username out of the longname and save it as shortname. If your network is set differently this code might have to change accordingly.
Let me know if it worked for you.
using (var p4 = new P4Connection())
{
p4.Connect();
var longName = WindowsIdentity.GetCurrent().Name;
var shortname = longName.Substring(longName.IndexOf("\\") + 1);
var records = p4.Run("clients", "-u", shortname);
cmbBoxPerforceWorkspaceLocation.Items.Clear();
foreach (P4Record record in records.Records)
{
cmbBoxPerforceWorkspaceLocation.Items.Add(record["client"]);
}
}
P4.Net is designed to be similar to the scripting APIs, which in turn are designed around the command line interface. It definitely does not have a intuitive object-oriented interface... which is off putting at first. But if you start from the command-line (esp -ztag flag) and piece together all data/actions your app needs, you will find it pretty easy to use P4.Net. And since it's similar to all the scripting APIs, you'll find it natural to pickup Python or Ruby if you wish :-)
Is there a way to get ALL valid resolutions for a given screen?
I currently have a dropdown that is populated with all valid screens (using Screen.AllScreens). When the user selects a screen, I'd like to present them with a second dropdown listing all valid resolutions for that display (not just the current resolution).
I think it should be possible to get the information using Windows Management Instrumentation (WMI). WMI is accessible from .NET using the classes from them System.Management namespace.
A solution will look similar to the following. I don't know WMI well and could not immediately find the information you are looking for, but I found the WMI class for the resolutions supported by the video card. The code requires referencing System.Management.dll and importing the System.Management namespace.
var scope = new ManagementScope();
var query = new ObjectQuery("SELECT * FROM CIM_VideoControllerResolution");
using (var searcher = new ManagementObjectSearcher(scope, query))
{
var results = searcher.Get();
foreach (var result in results)
{
Console.WriteLine(
"caption={0}, description={1} resolution={2}x{3} " +
"colors={4} refresh rate={5}|{6}|{7} scan mode={8}",
result["Caption"], result["Description"],
result["HorizontalResolution"],
result["VerticalResolution"],
result["NumberOfColors"],
result["MinRefreshRate"],
result["RefreshRate"],
result["MaxRefreshRate"],
result["ScanMode"]);
}
}
The following link contains detailed code examples for this:
Task 2: Changing the Display Resolution
http://msdn.microsoft.com/en-us/library/aa719104(VS.71).aspx#docum_topic2
The accepted answer doesn't seem to work on Windows 8.1, at least on my machine. The query runs fine but there are 0 entries in the results. And considering Bijoy K Jose's comment I suppose that I am not the only one.
However the validated answer for the following question worked out just fine :
How to list available video modes using C#?
Thanks to Vimvq1987