How to run application in an authenticated manner

蹲街弑〆低调 提交于 2020-01-24 12:10:09

问题


I've created a small application which attempts to authenticate a user based on their username and password. This application works correctly when run on the same domain which Active Directory resides on.

I must now extend the application to also work on domains which are "closed" in terms of security and permissions. In other words, is there a way to run the application based on an administrator account, or an account which has the necessary permissions to access the Active Directory?

This is the code I have used to authenticate a user:

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, server + ":" + port))
{
       if (pc.ValidateCredentials(username, password))
       {
              valid = true;
       }
       else 
       {
              valid = false;
       }
}

The above code works perfectly, however I would like to modify it so that it can communicate with the Active Directory database in an authenticated manner.

I have read numerous documentation and resources, but have not found anything. The closes I found was an article mentioning that IIS has to be set up and configured in a specific manner. However, my application is a simple C# application, and IIS is not being used.


回答1:


If I understand the question properly, you want to execute ValidateCredentials using a different user than the current process' user.

I may be missing something, but have you tried modifying your code this way?

using (PrincipalContext pc = 
        new PrincipalContext(ContextType.Domain, 
                             server + ":" + port, 
                             specialAccountUsername, 
                             specialAccountPassword))
{
       return pc.ValidateCredentials(username, password);
}

It simply uses a constructor that takes the special account you are using for accessing the domain.



来源:https://stackoverflow.com/questions/19838249/how-to-run-application-in-an-authenticated-manner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!