C# API for Powershell Get-ADDomain - c#

I am trying to find the relevant C# API for Powershell's Get-ADDomain. I don't want to invoke the Powershell commands in C#. Instead I am looking for one or multiple C# API with which I can retrieve all the values.
Get-ADDomain -Identity user.com
I tried searching through DOT net API's but couldn't find a relevant one. I found Domain class. But I am not sure how to get all the info using GetDomain
System.DirectoryServices.ActiveDirectory.Domain.GetDomain(DirectoryContext)
Can someone help in finding the relevant C# API that I can use to retrieve all the values of Get-ADDomain?

If you want to get the domain info for the domain that the computer is a member of or the domain that the user running the script is a member of (if they are the same, flip a coin), you can use a different method on the same class you're already trying to use:
For computer's domain:
[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()
For user's domain:
[System.DirectoryServices.ActiveDirectory.Domain]::GetUserDomain()
...yes it's in PowerShell but just invoke the either of the two methods above and they internally pass in the DirectoryContext relative to what you're looking for.
You might also get more information you're looking for by going up to the forest and retrieving that info as well:
[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()

Related

Poll for only password change in Active Directory

what is the recommended approach for polling password changes only in Active Directory, get the updated password and update else where ?
I have looked into classes in System.DirectoryServices.Protocols namespace like 'DirectoryNotificationControl' class but seems like this would poll for any attribute and later on we need to query to see if attribute we are interested (password) has indeed changed.
Also how do we get password value from active directory ? It is possible using LSA although very complex..
The only "simple" way (and those are VERY big quotes around "simple") is to write up a Password Filter Dll that is hosted on the Domain Controller.
You can implement the PasswordChangeNotify interface and have your code update whatever external thing that needed to be notified of the password change.
However this must be done in native code, so no C# allowed.
I originally went down this road and ended up giving up on it and used a different method of tracking the changed passwords, however one thing I did discover along the way was an open source project called passwdhk that may be helpful for you.
What passwdhk does is it implements a Password Filter DLL for you, however all the filter does is launch another executable with the command line arguments that where passed in to the password change notify function ("post-change program" is forwarding the arguments from PasswordChangeNotify, "pre-change program" is forwarding the arguments from PasswordFilter). This allows you to still write your code that updates the other service in C#, it just takes the password in from the command line instead of intercepting the password itself.
As far as I know, by default the password (stored in an encrypted way) cant't be read in Active-Directory. You can change the policy to store it in a reversible way, but it's really not a good thing.
The only way I know, is to install a componant on each client machine. The component catch the password change and you can do what you want.
From NT to XP this component was called GINA (DLL). Begining Vista this companent should be written using Credential Provider API.

Easiest way to get EC2 instance attributes within the instance itself

Can you guys show me how to retrieve instanceId, dns public name and type of current EC2 instance from where I'm running the code...
I'm playing with DescribeInstanceAttribute(), but it needs to supply the instanceId to the request, and I can't find how to get the id of currently running instance.
There's a much easier method than making a web API call, the .NET SDK.
See the SDK documentation for EC2InstanceMetadata here
For example, if you need InstanceId you can use:
Amazon.Util.EC2InstanceMetadata.InstanceId.ToString();
All the other properties are available in a similar manner.
Note that the SDK used to have Amazon.EC2.Utils - this was deprecated in 2015 and moved to Amazon.Util namespace
There's a webservice that returns machine information. Access
http://169.254.169.254/latest/meta-data/instance-id
To retrieve the instance id
The Instance Metadata Documentation can be handy.
You can also use AWS's Dot Net SDK for example Amazon.EC2.Util.EC2Metadata.InstanceId.ToString();

C# - Google Drive SDK - Storing client and secret ID in winform

I have written a C# winform program that uses the google drive SDK and that I want to release publicly. However, I'm not sure if it's safe to store my Google Drive client and secret ID's in the source code, and if it's not, what is the recommended way to store them?
Example, i have a class that explicitly defines two varialbes - String CLIENT_ID = and String SECRET_ID = . If I send out the .exe for this, is this information easily attained by someone?
Yes. Using a tool like reflector, you can pull out that information in a few seconds.
The answer is, don't use YOUR google drive. Make the user use their drive. Or, if it's something your program needs to just READ...well, make the file public.

accessing websites using C#

I have a problem here. Assume there's a basic calculator implemented in javascript hosted on a website ( I have googled it and to find an example and found this one: http://www.unitsconverter.net/calculator/ ). What I want to do is make a program that opens this website, enters some value and gets the return value. So, in our website calculator, the program:
- open the website
- enters an operand
- enters an operation
- enters an operand
- retrieve the result
Note: things should be done without the need to show anything to the user ( the browser for example ).
I did some search and found about HttpWebRequest and HttpWebRespond. But I think those can be used to post data to the server, which means, The file I'm sending data to must be php, aspx or jsp. But Javascript is client side. So, I think they are kind of useless to me in this case.
Any help?
Update:
I have managed to develop the web bot using WebBrowser Control tool ( found in System.Windows.Forms )
Here's a sample of the code:
webBrowser1.Navigate("LinkOfTheSiteYouWant"); // this will load the page specified in the string. You can add webBrowser1.ScriptErrorsSuppressed = true; to disable the script in a page
webBrowser1.Document.GetElementById("ElementId").SetAttribute("HTMLattrbute", "valueToBeSet");
Those are the main methods I have used to do what I wanted to.
I have found this video useful: http://www.youtube.com/watch?v=5P2KvFN_aLY
I guess you could use something like WatiN to pipe the user's input/output from your app to the website and return the results, but as another commenter pointed out, the value of this sort of thing when you could just write your own calculator fairly escapes me.
You'll need a JavaScript interpreter (engine) to parse all the JavaScript code on the page.
https://www.google.com/search?q=c%23+javascript+engine
What you're looking for is something more akin to a web service. The page you provided doesn't seem like it accepts any data in an HTTP POST and doesn't have any meaningful information in the source that you could scrape. If for example you wanted to programmatically make searches for eBay auctions, you could figure out how to correctly post data to it eg:
http://www.ebay.com/sch/i.html?_nkw=http+for+dummies&_sacat=267&_odkw=http+for+dummies&_osacat=0
and then look through the http response for the information you're looking for. You'd probably need to create a regular expression to match the markup you're looking for like if you wanted to know how many results, you'd search the http response for this bit of markup:
<div class="alt w"><div class="cnt">Your search returned <b>0 items.</b></div></div>
As far as clientside/javascript stuff, you just plain aren't going to be able to do anything like what you're going for.
It is a matter of API: "Does the remote website expose any API for the required functionality?".
Well web resources that expose interactive API are called web service. There are tons of examples (Google Maps for istance).
You can access the API -depending on the Terms & Conditions of the service- through a client. The nature of the client depends on the kind of web service you are accessing.
A SOAP based service is based on SOAP protocol.
A REST based service is based on REST principles.
So, if there is an accessible web service called "Calculator", then you can access the service and, for istance, invoke the sum method.
In your example, the calculator is a Javascript implementation, so it is not a web service and it cannot be accessed via HTTP requests. Though, its implementation is still accessible: it is the javascript file where the calculator is implemented. You can always include the file in your website and access its functions via javascript (always mind terms and conditions!!).
A very common example is the jQuery library stored in Google Libraries.

C# failing LDAP queries

I'm trying to access an LDAP directory via the SearchRequest object in C#. I can make the same calls via an LDAP library running in and iPhone app, as well as directly via a terminal session. However, the C# queries all seem to fail.
var search = new SearchRequest("ou=calendar,dc=ualberta,dc=ca", "term=*,course=094398,class=*", System.DirectoryServices.Protocols.SearchScope.Subtree, attributeLst);
This returns a list of terms for the course calendar. However, making the following calls won't return results for specific courses
var search = new SearchRequest("ou=calendar,dc=ualberta,dc=ca", "term=1330,course=094398", System.DirectoryServices.Protocols.SearchScope.Subtree, attributeLst);
The attributeLst object has proper attribute names included, but the query always returns with zero results.
Any suggestions anyone has would be greatly appreciated. Thanks.
Could it be related to the underlying LDAP property, i.e course's ldap datatype, i.e. is it one of the various strings or an integer in the LDAP store, if so the leading zero may throw it off? Also, I'm curious, logical and's (atleast when querying AD which is an LDAP implementation - not sure what your underlying store is) typically follow something like this:
(&(term=1330)(course=094398))

Categories

Resources