Link to home
Start Free TrialLog in
Avatar of igorlove
igorloveFlag for United States of America

asked on

How retreive the login user athentication info into system.net.NetworkCredential ( UserId, Password, Domain)

I have console application with the following code:

namespace ConsoleApplication1
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  //
                  // use one of our super secret domain accounts for authentication
                  System.Net.NetworkCredential networkcredential = new
                        System.Net.NetworkCredential
                        (
                           Login UserId,
                          Login Password,
                                                        Login Domain
                        );

                  System.Console.WriteLine(System.Net.CredentialCache.DefaultCredentials);
//                  System.Console.WriteLine(NetworkCredential);
                  //System.Console.WriteLine(localhost.ath_common.global.scao_athwebdev1_DLAA.getUserID());      
                  System.Net.WebRequest httpwebrequest = (HttpWebRequest) WebRequest.Create("https://cluster.netservers.com/g_database_server_information.xml");
                  httpwebrequest.Method = "GET";

                  httpwebrequest.Credentials = networkcredential;

                  HttpWebResponse webresponse = (HttpWebResponse) httpwebrequest.GetResponse();

                  System.Xml.XmlDocument xmldocument_response = new System.Xml.XmlDocument();
                  xmldocument_response.Load(webresponse.GetResponseStrea());
                        
                  webresponse.Close();
            }
      }
}
How can I retrieve UserId, Password and Domain for the Login user.
Avatar of devsolns
devsolns

Use CredentialCache.DefaultCredentials

The DefaultCredentials property applies only to NTLM, negotiate, and Kerberos-based authentication.

DefaultCredentials represents the system credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application. For ASP.NET applications, the default credentials are the user credentials of the logged-in user, or the user being impersonated.

more info:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetCredentialCacheClassDefaultCredentialsTopic.asp

-gp
Avatar of igorlove

ASKER

Hello devsolns,

It's resolve my problem, but  half.

I put this code in default.aspx file and run it against IIS server on my computer:
<%@ Page LANGUAGE="c#" %>

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Net"%>
<%


Response.Write("here I ha");

                  string url = "https://netserver.com/g_database_server_information.xml";
                  System.Net.HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                  myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
                    
                  System.Net.HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                  Response.Write("Authentication successfully");

                  System.Xml.XmlDocument xmldocument_response = new System.Xml.XmlDocument();
                  xmldocument_response.Load(myHttpWebResponse.GetResponseStream());
                        
                  myHttpWebResponse.Close();
                  Response.Write(xmldocument_response.OuterXml);

%>

and got error: " The remote server returned an error: (401) Unauthorized"
It's look like IIS server using different cridentials tnen login.
My first question: How I can get cridential for remote server?
and second one : How can I resolve the task to send web request from client with credentials to first remote server and the first remote server will send web request with theire cridentials to second remote server and get response?
Thanks
are you clients ALWAYS going to be inside of the domain?  this is important to determine because if not we can use windows authentication.
after we iron out a few thing we'll have a strategy to solve your problem so rest asure there is away we just need to find the best one...
Thank you for help.  I am waiting for problem solving ASAP.
The answer on the first question:
Yes, all clients and the servers are in the same domain always.
ASKER CERTIFIED SOLUTION
Avatar of devsolns
devsolns

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Gary Paulish,

Thank you very much for the excelent solution and help.

I will appriciate if you sent me some references to books or articles that help me understand this authentication process in general.
Absolutely, this will give you a great starting place to eventually ask more questions...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/pagexplained0001.asp

It will cover the above code and also introduce you to other forms of impersonation which are important because if you where building this application outside of a win domain you would need to use some of the other impersonation techniques.

The one you are using is by far the most secure.

Glad to help...
Thank you

Igor G.