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.

364<br />

How To: Use Forms Authentication with Active <strong>Directory</strong><br />

// Add the cookie to the outgoing cookies collection.<br />

Response.Cookies.Add(authCookie);<br />

// Redirect the user to the originally requested page<br />

Response.Redirect(<br />

FormsAuthentication.GetRedirectUrl(txtUserName.Text,<br />

false));<br />

}<br />

else<br />

{<br />

lblError.Text =<br />

"Authentication failed, check username and password.";<br />

}<br />

}<br />

catch(Exception ex)<br />

{<br />

lblError.Text = "Error authenticating. " + ex.Message;<br />

}<br />

6. Implement an Authentication Request Handler to Construct<br />

a GenericPrincipal Object<br />

<br />

This procedure implements the Application_AuthenticateRequest event handler<br />

within global.asax and creates a GenericPrincipal object for the currently authenticated<br />

user. This will contain the list of groups that the user is a member of, retrieved<br />

from the FormsAuthenticationTicket contained in the authentication cookie.<br />

Finally, you will associate the GenericPrincipal object with the current HttpContext<br />

object that is created for each Web request.<br />

To implement an authentication request handler to construct a GenericPricipal object<br />

1. Use Solution Explorer to open global.asax.cs.<br />

2. Add the following using statements to the top of the file.<br />

using System.Web.Security;<br />

using System.Security.Principal;<br />

3. Locate the Application_AuthenticateRequest event handler and add the following<br />

code to obtain the cookie that contains the encrypted<br />

FormsAuthenticationTicket, from the cookie collection passed with the request.<br />

// Extract the forms authentication cookie<br />

string cookieName = FormsAuthentication.FormsCookieName;<br />

HttpCookie authCookie = Context.Request.Cookies[cookieName];<br />

if(null == authCookie)<br />

{

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

Saved successfully!

Ooh no, something went wrong!