一键导入
oidc-hosted-page-dotnet
Implement "Sign in with SSO" in ASP.NET Core applications using SSOJet OIDC middleware.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Implement "Sign in with SSO" in ASP.NET Core applications using SSOJet OIDC middleware.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Implement Machine-to-Machine (M2M) authentication using SSOJet OAuth2 Client Credentials flow for backend services and daemons.
Implement "Sign in with SSO" in native Android/Kotlin applications using SSOJet OIDC with AppAuth.
Implement "Sign in with SSO" in Angular SPA applications using SSOJet OIDC with PKCE.
Implement "Sign in with SSO" in Go applications using SSOJet OIDC Authorization Code flow.
Implement "Sign in with SSO" in native iOS/Swift applications using SSOJet OIDC with AppAuth.
Implement "Sign in with SSO" in Java Spring Boot applications using SSOJet OIDC with Spring Security.
| name | oidc-hosted-page-dotnet |
| description | Implement "Sign in with SSO" in ASP.NET Core applications using SSOJet OIDC middleware. |
This expert AI assistant guide walks you through integrating "Sign in with SSO" functionality into an existing login page in a .NET Core application using SSOJet as an OIDC identity provider. The goal is to modify the existing login flow to add SSO support without disrupting the current traditional login functionality (e.g., email/password).
Microsoft.AspNetCore.Authentication.OpenIdConnect.http://localhost:5000/auth/callback).Run the following command to install the required NuGet package:
dotnet add package Microsoft.AspNetCore.Authentication.OpenIdConnect
Add the following to appsettings.json:
{
"SSOJet": {
"IssuerUrl": "https://auth.ssojet.com",
"ClientId": "your_client_id",
"ClientSecret": "your_client_secret",
"RedirectUri": "http://localhost:5000/auth/callback"
}
}
Update your Program.cs (or Startup.cs) to register the OIDC authentication scheme:
// Program.cs
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
var ssojetConfig = builder.Configuration.GetSection("SSOJet");
options.Authority = ssojetConfig["IssuerUrl"];
options.ClientId = ssojetConfig["ClientId"];
options.ClientSecret = ssojetConfig["ClientSecret"];
options.ResponseType = OpenIdConnectResponseType.Code;
options.CallbackPath = "/auth/callback";
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = context =>
{
// Pass login_hint if available
var loginHint = context.Properties.GetParameter<string>("login_hint");
if (!string.IsNullOrEmpty(loginHint))
{
context.ProtocolMessage.LoginHint = loginHint;
}
return Task.CompletedTask;
},
OnRemoteFailure = context =>
{
context.HandleResponse();
context.Response.Redirect("/login?error=oidc_failed");
return Task.CompletedTask;
}
};
});
var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Modify your existing login view (e.g., Views/Account/Login.cshtml) to include the "Sign in with SSO" toggle:
<!-- Views/Account/Login.cshtml -->
@{
ViewData["Title"] = "Sign In";
}
<div class="login-container">
<h1>Sign In</h1>
@if (ViewBag.Error != null)
{
<p style="color: red;">@ViewBag.Error</p>
}
<form id="loginForm" method="post" asp-action="Login" asp-controller="Account">
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" required />
</div>
<div id="passwordField">
<label for="password">Password</label>
<input type="password" id="password" name="password" required />
</div>
<input type="hidden" id="isSSO" name="isSSO" value="false" />
<button type="submit" id="submitBtn">Sign In</button>
</form>
<button type="button" id="ssoToggle" onclick="toggleSSO()">
Sign in with SSO
</button>
</div>
<script>
function toggleSSO() {
const isSSO = document.getElementById('isSSO');
const passwordField = document.getElementById('passwordField');
const submitBtn = document.getElementById('submitBtn');
const ssoToggle = document.getElementById('ssoToggle');
if (isSSO.value === 'false') {
isSSO.value = 'true';
passwordField.style.display = 'none';
document.getElementById('password').removeAttribute('required');
submitBtn.textContent = 'Continue with SSO';
ssoToggle.textContent = 'Back to Password Login';
} else {
isSSO.value = 'false';
passwordField.style.display = 'block';
document.getElementById('password').setAttribute('required', 'true');
submitBtn.textContent = 'Sign In';
ssoToggle.textContent = 'Sign in with SSO';
}
}
</script>
Create the AccountController to handle login and SSO:
// Controllers/AccountController.cs
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Mvc;
public class AccountController : Controller
{
[HttpGet]
public IActionResult Login(string error = null)
{
if (!string.IsNullOrEmpty(error))
{
ViewBag.Error = "SSO authentication failed. Please try again.";
}
return View();
}
[HttpPost]
public IActionResult Login(string email, string password, string isSSO)
{
if (isSSO == "true")
{
// Trigger SSO login with login_hint
var properties = new AuthenticationProperties
{
RedirectUri = "/dashboard",
};
properties.SetParameter("login_hint", email);
return Challenge(properties, OpenIdConnectDefaults.AuthenticationScheme);
}
// Existing password login logic here
Console.WriteLine("Processing traditional login...");
return Redirect("/dashboard");
}
[HttpGet]
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return Redirect("/login");
}
}
Dashboard Controller (Controllers/DashboardController.cs):
// Controllers/DashboardController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
[Authorize]
public class DashboardController : Controller
{
public IActionResult Index()
{
var claims = User.Claims.Select(c => new { c.Type, c.Value });
return View(claims);
}
}
dotnet run.http://localhost:5000/login)./auth/callback and then to /dashboard.OnRemoteFailure and OnAuthenticationFailed events for granular error handling.ClientSecret using the .NET Secret Manager (dotnet user-secrets) or Azure Key Vault.appsettings.Development.json for local overrides and never commit secrets to source control.ILogger) to debug OIDC flow issues.