How to Get System.Net.ICredential of Current User?

R

richard.martino

I have a desktop application in which I need to get the
System.Net.ICredential of the currently logged in user.

I do not care what the domain, username or password is. I just need
the credential in order to send an email via our in-house email
system.

I can easily get domain and username, but I do not want to prompt the
user for his password because he already logged in and he already
authorized to send an email.

BTW, System.Net.CredentialCache.DefaultCredentials
and ...DefaultNetworkCredentials do not work.

I think there might be a way to create this System.Net.ICredential
somehow from System.Security.Principal.WindowsIdentity.GetCurrent(),
but I have not, yet, found it.

Thanks.
 
J

Joe Kaplan

System.Net.CredentialCache.DefaultCredentials IS the ICredential of the
current user on the executing thread, so that is what you should be using.
Why do you say this does not work?

Joe K.
 
R

richard.martino

Why do you say this does not work?

-----------------------

Because when I execute:

sendmailproxy.SendEmailRequest(sendmail);

I get:

System.Net.WebException from System.Web.Services

The request failed with HTTP status 401: Unauthorized.


Remember, this web service that I am using is a produced locally at
the company where I work, and I have access to their developers. Is
there some [web attribute] or something on their end that they need to
enable or disable?

Thanks
 
R

richard.martino

Also,

On my computer System.Net.CredentialCache.DefaultCredentials is
System.Net.NetworkCredential in which all three (3) fields are
System.String.Empty:

1. Domain
2. Password
3. UserName

Do I need to configure something on my computer to get data populated
into these three fields?

BTW, I would expect the Password field to be encrypted; otherwise a
user application can get the password and save it to a database or
something.
 
J

Joe Kaplan

Did you set the Credentials property of your web service proxy to
DefaultCredentials? I assume you did this, but your code didn't show it, so
thought I'd ask.

If you did that and it still fails, then the next thing to do would be to
examine the security event log on the remote server you are contacting and
find out what happened with the authentication. You would want to make sure
they have auditing enabled for logon events (success and failure) so you can
see what credentials it attempted to use when you logged in.

This also assumes that there is some sort of a trust relationship between
the user executing the local code and the remote server (same domain, etc.).

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
Why do you say this does not work?

-----------------------

Because when I execute:

sendmailproxy.SendEmailRequest(sendmail);

I get:

System.Net.WebException from System.Web.Services

The request failed with HTTP status 401: Unauthorized.


Remember, this web service that I am using is a produced locally at
the company where I work, and I have access to their developers. Is
there some [web attribute] or something on their end that they need to
enable or disable?

Thanks
 
H

Henning Krause [MVP - Exchange]

Hi,

additionally to what Joe wrote... does the WebService accept NTLM or
Kerberos authentication? If only basic authentication is enabled, the
default credentials cannot be used.

Kind regards,
Henning Krause

Joe Kaplan said:
Did you set the Credentials property of your web service proxy to
DefaultCredentials? I assume you did this, but your code didn't show it,
so thought I'd ask.

If you did that and it still fails, then the next thing to do would be to
examine the security event log on the remote server you are contacting and
find out what happened with the authentication. You would want to make
sure they have auditing enabled for logon events (success and failure) so
you can see what credentials it attempted to use when you logged in.

This also assumes that there is some sort of a trust relationship between
the user executing the local code and the remote server (same domain,
etc.).

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services
Programming"
http://www.directoryprogramming.net
--
Why do you say this does not work?

-----------------------

Because when I execute:

sendmailproxy.SendEmailRequest(sendmail);

I get:

System.Net.WebException from System.Web.Services

The request failed with HTTP status 401: Unauthorized.


Remember, this web service that I am using is a produced locally at
the company where I work, and I have access to their developers. Is
there some [web attribute] or something on their end that they need to
enable or disable?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top