![Screenshot 2024-05-10 at 11 59 05 AM](https://private-user-images.githubusercontent.com/32936811/329692404-d9487528-727a-4cf1-8607-ae5731305c76.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxNjUxMTAsIm5iZiI6MTczOTE2NDgxMCwicGF0aCI6Ii8zMjkzNjgxMS8zMjk2OTI0MDQtZDk0ODc1MjgtNzI3YS00Y2YxLTg2MDctYWU1NzMxMzA1Yzc2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEwVDA1MjAxMFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTllNWI0NDEwZWIxOTViNzRjN2E2NGRmZWM0MjY1NDg5ZWVlYjEyN2M1ZDhlMWY3YTVkMjNiZWYyZjU4Mjc3ZGYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.q_7XNkzOCXdOmsn53WbbSubgL4l3JrWqHFKwLwaqplg)
This sample application demonstrates the integration of Descope with a .NET Framework 4.8 backend and a traditional ASP.NET web application using JavaScript for user authentication flows.
- Features
- Installation
- Running the Application
- Environment Setup
- API Protection with TokenValidator
- Using Descope Web Component
- Issue Reporting
- License
Clone the repository:
git clone https://github.com/descope-sample-apps/dotnet-4.8-sample-app
Navigate to the cloned repository directory. Install dependencies and build the solution by opening the .sln
file in Visual Studio and restoring NuGet packages.
To start the application:
- Open the solution file (
.sln
) in Visual Studio. - Set the
DescopeProjectId
environment variable (see Environment Setup). - Run the solution (F5 or the "Start" button in Visual Studio).
- Set the
DESCOPE_PROJECT_ID
environment variable:
- Windows:
setx DESCOPE_PROJECT_ID "YOUR_DESCOPE_PROJECT_ID"
Replace YOUR_DESCOPE_PROJECT_ID
with your actual Descope Project ID.
- Place your Descope Project ID in the SDK initialization in the
AuthenticatedPage.aspx
, so that the web component will use your own flows:
const sdk = Descope({ projectId: "YOUR_DESCOPE_PROJECT_ID", persistTokens: true, autoRefresh: true });
The TokenValidator
class is used to secure API endpoints by validating JWT tokens, passed to your backend as a Bearer Token. Here’s an example of how to protect an API controller:
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.IdentityModel.Tokens;
namespace DescopeSampleApp.Controllers {
public class SampleController : ApiController
{
public async Task<IHttpActionResult> Get()
{
var authorizationHeader = Request.Headers.Authorization;
if (authorizationHeader != null && authorizationHeader.Scheme.Equals("Bearer", StringComparison.OrdinalIgnoreCase))
{
var sessionToken = authorizationHeader.Parameter;
if (!string.IsNullOrEmpty(sessionToken))
{
// Validate the session token
var tokenValidator = new TokenValidator("YOUR_DESCOPE_PROJECT_ID");
try
{
var claimsPrincipal = await tokenValidator.ValidateSession(sessionToken);
return Ok("This is a sample API endpoint.");
}
catch (SecurityTokenValidationException)
{
return Unauthorized();
}
}
}
return Unauthorized();
}
}
}
In the AuthenticatedPage.aspx
file, use the Descope Web SDK to handle user authentication:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="DescopeSampleApp.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://unpkg.com/@descope/[email protected]/dist/index.umd.js"></script>
</head>
<body>
<form id="loginForm" runat="server">
<p>Welcome to the Authenticated Page!</p>
</form>
<script>
const sdk = Descope({ projectId: "YOUR_DESCOPE_PROJECT_ID", persistTokens: true, autoRefresh: true });
const sessionToken = sdk.getSessionToken()
const currentPath = window.location.pathname;
console.log(currentPath)
if ((sessionToken) && (!sdk.isJwtExpired(sessionToken))) {
// User is logged in
} else {
if (currentPath != '/login.aspx') {
// Redirect to login page
window.location.replace('/login.aspx');
}
}
</script>
</body>
</html>
For any issues or suggestions, please open an issue on GitHub.
This project is licensed under the MIT License - see the LICENSE file for details.