Implementing SSO

Implementing Single Sign-On (SSO) in YourSite

Integrating Single Sign-On (SSO) with K12NET in your ASP.NET Core web application "YourSite" enables users to authenticate using their K12NET credentials. This guide walks you through setting up a login link on your Index.cshtml page and configuring the necessary parameters for SSO.

Preparing the Login Link

The Index.cshtml page includes a "Login with K12NET" link that directs users to the K12NET authorization endpoint. Here's the HTML for the link:


    <‍a href="@(ViewData["url"])/GWCore.Web/connect/authorize?response_type=code&client_id=@(ViewData["client_id"])[email protected]((string)ViewData["redirect_uri"])&scope=openid profile">Login with K12NET<‍/a>

This link is dynamically constructed using configuration values for the authorization URL, client ID, and redirect URI, ensuring users are directed to the correct K12NET login page with your application's specific parameters.

Configuring the Code Behind

In the IndexModel class, which backs the Index.cshtml page, configuration values are retrieved from the application's settings and passed to the view via ViewData. Here's the C# code for the IndexModel class:


using Microsoft.AspNetCore.Mvc.RazorPages;

namespace YourSite.Pages
{
    public class IndexModel : PageModel
    {
        private readonly IConfiguration Configuration;

        public IndexModel(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void OnGet()
        {
            ViewData["url"] = Configuration.GetSection("K12NETPartnerInfo:url").Value;
            ViewData["client_id"] = Configuration.GetSection("K12NETPartnerInfo:client_id").Value;
            ViewData["redirect_uri"] = Configuration.GetSection("K12NETPartnerInfo:redirect_uri").Value;
        }
    }
}

This class uses IConfiguration to access application settings defined in appsettings.json or another configuration source. The OnGet method populates ViewData with the authorization URL, client ID, and redirect URI from the configuration. These values are essential for constructing the SSO link.

Setting Up Configuration

Ensure your appsettings.json file includes the necessary K12NET partner information under the K12NETPartnerInfo section:


  "K12NETPartnerInfo": {
    "url": "https://api.k12net.com",
    "client_id": "your_client_id_here",
    "client_secret": "your_client_secret_here",
    "redirect_uri": "your_redirect_uri_here"
  }

Replace your_client_id_here and your_redirect_uri_here with your actual K12NET client ID and redirect URI.

Conclusion

By following these steps, you have implemented a "Login with K12NET" link in your ASP.NET Core web application, "YourSite." This setup initiates the SSO process, allowing users to authenticate with their K12NET credentials. The seamless integration not only enhances the user experience but also ensures secure access to your application.