Remote Connection using Management Scope? - c#

Am trying to connect the remote system from my local machine with the help of Management Scope.
What i tried is:
My code as follows,
ConnectionOptions con= new ConnectionOptions();
con.Username = strUsername;
con.Password = strPassword;
con.Impersonation = ImpersonationLevel.Impersonate;
con.EnablePrivileges = true;
ManagementScope ManagementScope1 = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", strIP), con);
ManagementScope1.Connect();
My Problem is:
With the preceding code, I can connect with remote system when It has some username and password.(strUsername = "User", strPassword="Welcome")
But in case of blank password (strUsername = "User", strPassword = "") , am getting the error named as "Access is Denied". The connecting remote system has no protection from the password.
Remote system user gave all sorts of permission to access the machine, but still am facing with this strange issue.
please let me know where am going wrong. Any help is much appreciated. Thanks in advance.

If you want to use currently logged user to access remote WMI, you have to pass null as Username.
null if the connection will use the currently logged-on user; otherwise, a string representing the user name. The default value is null.
Source: http://msdn.microsoft.com/en-US/library/system.management.connectionoptions.username(v=vs.110).aspx

Related

Copy files to a network drive (without Impersonation)

I have a WindowsForms vb.net Framework 4.6 Application which must copy some files from c:\sample to a network drive (\\servername\sample).
I must use other credentials to access the share. But I can't use impersonate, because i have to use a local user on the Server. The Login on the server is something like <.\Username> <password>.
Is there a way to copy my files to the network drive using the server's local user credentials?
EDIT 18.06:
I also tried this way, but it did not work for me.
Dim domain, username, passwordStr, remoteServerName As String
Dim password As New System.Security.SecureString
Dim command As New Process
domain = "servername"
username = "username"
passwordStr = "password"
remoteServerName = "\\servername\path\"
Dim impersonator As New AliasAccount(username, passwordStr)
For Each c As Char In passwordStr.ToCharArray
password.AppendChar(c)
Next
command.StartInfo.FileName = "test.pdf"
command.StartInfo.Arguments = remoteServerName
command.StartInfo.UserName = username
command.StartInfo.Password = password
command.StartInfo.Domain = domain
command.StartInfo.Verb = "open"
command.StartInfo.UseShellExecute = False
impersonator.BeginImpersonation()
command.Start()
impersonator.EndImpersonation()
My user on the server does not a have a specific domain, because the user only exists on the server. So i'm using the servername as the domain. I also tried using ".\".
I'm always getting the same error "wrong username or password" (translated from german to englisch). I've checked the username and password more than 30 times... It is definitely correct. I'm also able to connect a network drive with the credentials... So they are correct.
Do you have any other suggestions?

The user name or password is incorrect-8009030C: LdapErr: DSID-0C090579, comment: AcceptSecurityContext error, data 52e, v3839

I am trying to connect AD with the following code
VMADProfile _VMADProfile = new VMADProfile();
DirectoryEntry searchRoot = new DirectoryEntry("Test AD LDAP Path", "DomainName\\username", "password");
directorySearcher.Filter = "(&(objectCategory=user)(samAccountName=" + username + "))";
directorySearcher.PropertiesToLoad.Add("displayname");
SearchResult one = directorySearcher.FindOne();
but it is giving me following Error
The user name or password is incorrect.
8009030C: LdapErr: DSID-0C090579, comment: AcceptSecurityContext error, data 52e, v3839
I am able to login with same user name and password if I login directly.
My AD that I am trying to connect is different from from which i am running code. Same code works fine if i change test AD to the production AD
I think this is the key:
My AD that I am trying to connect is different from from which i am running code.
It's probably trying to authenticate you on your domain, not the other one. Make sure your LDAP path includes the server to connect to for the other domain, and not just the distinguished name of the domain. The server name can just be the DNS name for the domain. It should look something like this:
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://otherdomain.com/DC=otherdomain,DC=com", "DomainName\\username", "password");
If you just use "LDAP://DC=otherdomain,DC=com", it will try to connect to the domain the current computer is joined to first. That would actually work if there is a trust between the two domains, but if not, you need to specifically give it the server name to connect to.

autenticating ldap for web applications

please take a few minutes and read my question completely. here is my problem :
I want to connect to LDAP server by C# for a web application, means clients connecting to the asp.net server.
*- The ldap server and application server are not the same.
**- They are not in a same domain.
I have been trying 4 different ways and could not solve the problem by none of them.
1-
var credentials = new NetworkCredential(username, password);
var serverId = new LdapDirectoryIdentifier("domain.net");
var conn = new LdapConnection(serverId, credentials);
conn.Bind();
2-
System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://domain.net/DC=domain,DC=net");
entry.Username = "username";
entry.Password = "password";
System.DirectoryServices.DirectorySearcher searcher = new System.DirectoryServices.DirectorySearcher(entry);
searcher.Filter = "(&(objectClass=user))";
var results = searcher.FindAll();
the problem with these 2 ways is that the user must have an access to the server for login and we know that there is only admin of the system who has the permission.
3-
PrincipalContext pc = new PrincipalContext(ContextType.Domain, "domain.net");
var ret = pc.ValidateCredentials(model.UserName, model.Password);
the problem is the server must be in the domain of ldap server. we have this limitation !!
4-
https://auth0.com/blog/using-ldap-with-c-sharp/
public bool validateUser(string username, string password)
{
var sha1 = new SHA1Managed();
var digest = Convert.ToBase64String(sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)));
var request = new CompareRequest(string.Format("uid={0},ou=users,dc=example,dc=com", username),
"userPassword", "{SHA}" + digest);
var response = (CompareResponse)connection.SendRequest(request);
return response.ResultCode == ResultCode.CompareTrue;
}
this code does not return any thing. it will be very helpful if there is a query to compare usernames and passwords. this code seems to use this way but there are different types of hash algorithms. I tried to use SHA1 and MD5, and userPassword , unicodePwd attribute. but the return is empty all the time.
is it the best solution to put both servers in a same domain? any other solution , Thank you so much.
Method 1 is the simplest way. If that doesn't work, nothing will. So you will have to change your configuration to make it work.
When you say:
the user must have an access to the server for login and we know that there is only admin of the system who has the permission.
What do you mean? Are you explicitly denying login rights to the domain controllers? Are there network issues between the computer this is running on and the domain controller (can you hit port 389 on the domain controller(s))?
Update: PrincipalContext.ValidateCredentials just does an LDAP bind in behind - it uses LdapConnection. You can see the source code here. The probable reason why ValidateCredentials is working on a domain machine and not otherwise is because it uses Kerberos authentication by default, which will only work from a domain computer.
The same is true with LdapConnection. So try setting the authentication mode. For example, try Basic
var credentials = new NetworkCredential(username, password);
var serverId = new LdapDirectoryIdentifier("domain.net");
var conn = new LdapConnection(serverId, credentials, AuthType.Basic);
conn.Bind();
Or look at the available values and try what works.

MySql access denied for user - can't create the user the software is looking for

My connection string looks like this:
string server = "localhost";
string database = "stman";
string user = "logan";
string pass = "root";
string connStr = String.Format("Data Source={0};Initial Catalog={1}; User Id={2}; Password={3};", server, database, user, pass);
Given my understanding, this means MySql should be using 'logan'#'localhost' for the login, but it isn't.
It looks as though it's using 'logan'#<server's fully qualified name>:
Access denied for user 'logan'#'HP-PL-ML110.young.home'
I'm at a loss right now. SqlYog doesn't allow me to create a user with that hostname, my options for hostname are %, localhost or 127.0.0.1
Can anyone help me make sure that the website is using the specified username here? I have no idea what to look at to fix this
Joe's comment:
Have you tried disabling host name lookup, as in this question
Applying this didn't fix the issue but it did change the settings so that I could use the IP address in the connection string.
This does mean I had to create a new user (I found that, in Sqlyog, you can enter something different in the hostname field in user creation) and grant the new user the required permissions on the target database.
It's not exactly a solution, but it is a viable workaround that's got me back to a point where I can actually progress further in my development now.
Thanks Joe
string connStr = String.Format("Data Source={0};Initial Catalog={1}; User Id={2}; Password={4};", server, database, user, pass);
Should be
string connStr = String.Format("Server={0};Database={1};Uid={2};Pwd={4};", server, database, user, pass);

Cannot retrieve perforce clients

I am trying to develop a C# based build tool using p4api.net apis. I am new to perforce p4api.net. I followed the instructions given in p4api.net library downloaded from their site, but was never successful in running a basic command on perforce. I am attaching piece of code which is supposed to fetch clients from Perforce. Please correct it, if am wrong. The code throws a run time error (unhandled expection) while executing GetClients().
static void Main(string[] args)
{
string uri = "perforce:1666";
string user = "User1";
Server server = new Server(new ServerAddress(uri));
Repository rep = new Repository(server);
Connection con = rep.Connection;
con.UserName = user;
con.Client = new Client();
// connect to the server
con.Connect(null);
// run the command against the current repository
IList<Client> changes = rep.GetClients(null);
}
Any useful guide to perforce C# documents/examples would be appreciated.
Thanks,
Madhu
tickets are granted to the user by p4 login. If you login with P4V or another client the ticket should still be valid until expiration unless you explicitly p4 logout. You can create this credential in P4API.NET after you connect and before you run a command:
// connect to the server
con.Connect(null);
string password = "pw";
Credential cred = con.Login(password, null, null);
con.Credential = cred;
Are you sure that the exception is coming from GetClients? I ran your code successfully, but when I changed uri to a non-existent server:port I get the unhandled exception at con.Connect(null).
Confirm that you do have access to a perforce:1666 server with User1 and that User1 does not require a password on that server.

Categories