28.12.2013 Views

Building Secure ASP.NET Applications - People Search Directory

Building Secure ASP.NET Applications - People Search Directory

Building Secure ASP.NET Applications - People Search Directory

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11: .<strong>NET</strong> Remoting Security 273<br />

You can either specify the use of DefaultCredentials in a configuration file or set<br />

the credentials programmatically.<br />

Explicit Configuration<br />

Within the client application configuration file (Web.config, if the client application<br />

is an <strong>ASP</strong>.<strong>NET</strong> Web application) set the useDefaultCredentials attribute on the<br />

element to true in order to specify that the proxy should use<br />

DefaultCredentials when it communicates with the remote object.<br />

<br />

Programmatic Configuration<br />

For programmatic configuration, use the following code to establish the use of<br />

DefaultCredentials programmatically.<br />

IDictionary channelProperties;<br />

channelProperties = ChannelServices.GetChannelSinkProperties(proxy);<br />

channelProperties ["credentials"] = CredentialCache.DefaultCredentials;<br />

Using Specific Credentials<br />

To use a specific set of credentials for authentication when you call a remote object,<br />

disable the use of default credentials within the configuration file, by using the<br />

following setting.<br />

<br />

Note: Programmatic settings always override the settings in the configuration file.<br />

Then, use the following code to configure the proxy to use specific credentials.<br />

IDictionary channelProperties =<br />

ChannelServices.GetChannelSinkProperties(proxy);<br />

NetworkCredential credentials;<br />

credentials = new NetworkCredential("username", "password", "domain");<br />

ObjRef objectReference = RemotingServices.Marshal(proxy);<br />

Uri objectUri = new Uri(objectReference.URI);<br />

CredentialCache credCache = new CredentialCache();<br />

// Substitute "authenticationType" with "Negotiate", "Basic", "Digest",<br />

// "Kerberos" or "NTLM"<br />

credCache.Add(objectUri, "authenticationType", credentials);<br />

channelProperties["credentials"] = credCache;<br />

channelProperties["preauthenticate"] = true;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!