Password protecting your pages with .NET and AUthenticate (CAS)


Adding CAS to your .NET application can be done with a few simple steps. Full implementation details are available at https://wiki.jasig.org/display/CASC/.Net+Cas+Client but for purposes of Auburn sites do the following:

  1. Add the DotNetCasClient Reference to your .NET application
    1. Download the zip and extract
    2. Add the reference to your project using Visual Studio
  2. Modify your web.config
    1. Add  the following to you’re the <configuration> section:
      <configSections>
        <section name="casClientConfig" ;type="DotNetCasClient.Configuration.CasClientConfiguration,  DotNetCasClient"/>
        <!--  Other custom sections here -->
      </configSections>
      
    2. Add the <casClientConfig> element to the <configuration> section like so:
      <casClientConfig 
        casServerLoginUrl="https://authenticate.auburn.edu/cas/login" 
        casServerUrlPrefix="https://authenticate.auburn.edu/cas/" 
        serverName="https://{YourAuburnURL}" 
        notAuthorizedUrl="~/NotAuthorized.aspx" 
        cookiesRequiredUrl="~/CookiesRequired.aspx" 
        redirectAfterValidation="true" 
        renew="false" 
        singleSignOut="true" 
        ticketValidatorName="Cas20" 
        serviceTicketManager="CacheServiceTicketManager" />
      
    3. Register the module  within the <system.web><httpModules> and <system.webServer><modules> like so:
      <httpModules>
        <add name="DotNetCasClient" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient"/>
        <!-- Other modules here -->
      </httpModules>
      

      and

      <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules>
          <remove name="DotNetCasClient"/>
          <add 
            name="DotNetCasClient" 
            type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient"/>
          <!-- Other modules here -->
      </modules> </system.webServer>
    4. Configure Forms Authentication under the <system.web> section:
      <authentication  mode="Forms">
        <forms
          loginUrl="https://authenticate.auburn.edu/cas/login-d" 
          timeout="30" 
          defaultUrl="~{YourDefaultPage}" 
          cookieless="UseCookies" 
          slidingExpiration="true" 
          path="/{YourSiteName}/" />
      </authentication>
       
  3. For MVC applications Use the Authorize Attribute to Authenticate  and the FormsService.SignOut() function to invalidate that Authentication certificate:
    //this attribute will redirect the user to  authenticate if they aren't already
    //authorize is a misnomer in this case as this  doesn't manage user authorization at all
    [Authorize]
    public ActionResult LogOn() {
      return  RedirectToAction("Index", "Home");
    }
    public ActionResult LogOff() {
      FormsService.SignOut();
      return  RedirectToAction("Index", "Home");
    }
    
  4. Retrieve the logged in users info, perhaps for authorization:
    public ActionResult Index() {
      //this.ControllerContext.HttpContext is the alternative for HttpContext.Current.User in webforms
      string username =  this.ControllerContext.HttpContext.User.Identity.Name;
      return View();
    }