Concatenate automatically a prefix and a suffix to the username

Sep 9, 2009 at 9:55 AM

How can I to concatenate automatically a prefix and sufix to the username. I need to dial with the logon credentiasl but to the username I need concatenate a suffix and prefix and I don`t know how configurate.

 

Thanks

Coordinator
Sep 9, 2009 at 5:04 PM
using System;
using System.Net;

public static NetworkCredential BuildCredential(NetworkCredential baseCredential, string prefix, string suffix)
{
    return new NetworkCredential(
        string.Format("{0}{1}{2}", prefix, baseCredential.UserName, suffix),
        baseCredential.Password,
        baseCredential.Domain);
}

I would probably do something similar to that, and pass the resulting credential into the dialer.

Sep 10, 2009 at 1:53 PM

Ok, that isn´t the problem. My problem is transform WindowsIdentity into NetworkCredentials. I have got recover the user used to logon in the computer but y cannot convert credentials into networkcredentials. How I commented in the first post the problem is add de suffix to the user named logged in the windows system.

This code return the WindowsIdentuty like null and then I cannot convert into NetworkCredentials:

                WindowsImpersonationContext wic = WindowsIdentity.GetCurrent().Impersonate();
                ICredentials credentials = CredentialCache.DefaultCredentials;
                NetworkCredential netc = (NetworkCredential)credentials;
                BuildCredential(netc, "PREFIX", "SUFFIX");
                dialer.Dial(netc);

 

Thanks

Coordinator
Sep 10, 2009 at 2:57 PM
Edited Sep 10, 2009 at 3:06 PM

That's because the two objects aren't compatible. If you're trying to use Windows authentication that's determined at the phone book entry level, not something you'd pass into the dialer yourself. You need to set the UseLogOnCredentials flag on the RasEntry.Options property when you're creating the entry, or go to the security tab in the connection properties and check the "Automatically use my Windows logon name and password (and domain if any)" checkbox.  You'll also need to call Dial() or DialAsync() without specifying any credentials.

The way RAS authentication works is I have to provide the username, password, and domain (if applicable) to the dialer and then those are passed to the server by RAS during authentication. This would make the password for the user unsecure which would be a security violation, and probably why Microsoft decided a flag would be better in RAS to tell the operating system to do it rather than the caller.

Edit: Your idea to add the prefix and suffix to the credential isn't going to work since you can't retrieve the password from the credential object, nor would I be able to inside the dialer to pass the password into RAS. The only option you have for Windows authentication is to set the flag I mentioned in this reply.