Certainly! Here's a specific full example of how you can use the Apereo DotNetCasClient in an ASP .NET Web Forms application:
-
Start by creating a new ASP .NET Web Forms project in Visual Studio.
-
Install the Apereo DotNetCasClient package using the NuGet Package Manager Console:
Install-Package DotNetCasClient
- In your web.config file, add the following configuration for the CasClient:
<configuration>
<configSections>
<section name="casClientConfig" type="DotNetCasClient.Configuration.CasClientConfiguration, DotNetCasClient" />
</configSections>
<casClientConfig casServerLoginUrl="https://your-cas-server-url.com/cas/login"
casServerUrlPrefix="https://your-cas-server-url.com/cas/"
serverName="https://your-app-url.com"
serviceTicketManager="CacheServiceTicketManager"
gateway="false"
renew="false"
singleSignOut="true"
ticketTimeout="5000"
ticketValidatorName="Cas20" />
<system.web>
<httpModules>
<add name="CASAuthenticationModule" type="DotNetCasClient.CasAuthenticationModule,DotNetCasClient" />
</httpModules>
</system.web>
</configuration>
- In your Login.aspx file, add the following code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="WebFormsApp.Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Login</h2>
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
</div>
</form>
</body>
</html>
- In your Login.aspx.cs file, add the following code:
using System;
namespace WebFormsApp
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
// Redirect to CAS login page
Response.Redirect("~/Login.aspx?service=home");
}
}
}
- In your Home.aspx file, add the following code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="WebFormsApp.Home" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Home</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Welcome to the Home Page!</h2>
<asp:Label ID="lblUsername" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
- In your Home.aspx.cs file, add the following code:
using System;
using DotNetCasClient;
namespace WebFormsApp
{
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!CasAuthentication.CheckAuthentication())
{
// User not authenticated, redirect to CAS login page
Response.Redirect("~/Login.aspx?service=home");
}
// User authenticated, continue with page logic
// Retrieve username using CasAuthentication.User.Identity.Name
string username = CasAuthentication.User.Identity.Name;
lblUsername.Text = "Welcome, " + username;
}
}
}
That's it! You now have a basic ASP .NET Web Forms application that uses the Apereo DotNetCasClient for CAS authentication. When you run the application and click the "Login" button, it will redirect you to the CAS login page. After successful authentication, you'll be redirected back to the Home page and see a "Welcome, [username]" message.