您的位置:首页 > 健康 > 养生 > 华宇网站建设_浙江城乡和住房建设网_搜索引擎优化英文简称_百度推广竞价排名技巧

华宇网站建设_浙江城乡和住房建设网_搜索引擎优化英文简称_百度推广竞价排名技巧

2025/4/1 4:46:33 来源:https://blog.csdn.net/Marzlam/article/details/146474911  浏览:    关键词:华宇网站建设_浙江城乡和住房建设网_搜索引擎优化英文简称_百度推广竞价排名技巧
华宇网站建设_浙江城乡和住房建设网_搜索引擎优化英文简称_百度推广竞价排名技巧
SSO单点登录目的

之前一般来讲系统简单,登录后 本地 cookie 加服务器 session 存储用户身份信息,以此为依据来判断用户再次登录时免验证
但随着互联网发展,很多应用 部署在不同的服务器上,而用户体系是一套,那么按照原来的方式,每个应用的服务器都需要 登录然后存session信息,这样就导致了重复操作,而sso就是为了 在多个应用 多个服务器 还只想登录一次的效果 避免重复操作。
所以 目的很简单,在多服务器中 只通过一个认证登录的地方去处理

关键技术组件

令牌(Token):传递用户身份信息的载体(如 JWT、SAML Token)。
认证中心(Identity Provider, IdP):负责用户身份验证和令牌签发。
服务提供者(Service Provider, SP):依赖认证中心验证令牌的应用系统

Net 处理单点登录的方式配置认证中心 IdentityServer
  1. 基于 OAuth 2.0/OpenID Connect
    协议特点:
    OAuth 2.0:授权框架,用于第三方应用获取资源访问权限。
    OpenID Connect (OIDC):基于 OAuth 2.0 的身份认证层,支持 SSO。
    适用场景:互联网应用、跨平台服务(如第三方登录)。

  2. 场景说明
    假设企业使用 独立身份提供者(IdP)(如 IdentityServer、Azure AD 或 Okta)管理用户身份,多个应用(Service Providers, SP)通过该 IdP 实现 SSO。例如:
    应用A:https://app1.company.com(内部 HR 系统)
    应用B:https://app2.company.com(项目管理平台)
    用户登录任意应用后,可无缝访问其他应用,无需重复认证
    注!!登录信息的输入凭证都是在IDP输入不在应用A和应用B输入

  3. 实现步骤(以 OIDC + ASP.NET Core 为例)
    配置 Identity Provider(以 IdentityServer 为例)
    步骤:

    1. 部署 IdentityServer(开源 IdP)。
    2. 注册客户端(应用A和应用B)并配置重定向 URI。
    3. 定义用户身份信息(Claims)和 API 资源(Scopes)
// IdentityServer 的 Config.cs
public static class Clients
{public static IEnumerable<Client> GetClients(){return new List<Client>{new Client{ClientId = "app1",ClientSecrets = { new Secret("app1-secret".Sha256()) },AllowedGrantTypes = GrantTypes.Code,RedirectUris = { "https://app1.company.com/signin-oidc" },AllowedScopes = { "openid", "profile", "email" }},new Client{ClientId = "app2",ClientSecrets = { new Secret("app2-secret".Sha256()) },AllowedGrantTypes = GrantTypes.Code,RedirectUris = { "https://app2.company.com/signin-oidc" },AllowedScopes = { "openid", "profile", "email" }}};}
}

ASP.NET Core 代码配置:
在 Startup.cs 中添加 OIDC 认证中间件

// ASP.NET Core 中配置 OIDC 认证
// 完整 OIDC 配置示例(Startup.cs)
services.AddAuthentication(options =>
{options.DefaultScheme = "Cookies";options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{options.Authority = "https://idp.company.com";options.ClientId = "app1";options.ClientSecret = "app1-secret";options.ResponseType = "code";options.SaveTokens = true;options.UsePkce = true;options.Scope.Add("openid");options.Scope.Add("profile");options.Scope.Add("offline_access");options.TokenValidationParameters = new TokenValidationParameters{NameClaimType = "name",RoleClaimType = "role"};
});
  1. 登录控制器以及令牌验证:
public class AccountController : Controller
{// 触发登录(若未认证,自动跳转至 IdP)public IActionResult Login(string returnUrl = "/"){if (!User.Identity.IsAuthenticated){return Challenge(new AuthenticationProperties { RedirectUri = returnUrl });}return Redirect(returnUrl);}// 注销(本地 + IdP 全局注销)public async Task<IActionResult> Logout(){await HttpContext.SignOutAsync("Cookies");return SignOut(new AuthenticationProperties { RedirectUri = "/" }, "oidc");}
}[ApiController]
[Route("api/[controller]")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class UserController : ControllerBase
{[HttpGet("info")]public IActionResult GetUserInfo(){var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;return Ok(new { UserId = userId });}
}//JWT令牌验证
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>{options.Authority = "https://idp.company.com";options.Audience = "my-api-resource";options.TokenValidationParameters = new TokenValidationParameters{ValidateIssuer = true,ValidateAudience = true,ValidateLifetime = true};});
Net 处理单点登录的方式 共享cookie
  1. 共享cookie
    1. 场景说明
      假设一个企业拥有两个子应用:
      应用A:app1.example.com(员工考勤系统)
      应用B:app2.example.com(内部文档管理系统)
      用户只需登录一次,即可在两个应用间无缝切换。以下是具体实现流程。

    2. 实现原理
      将认证 Cookie 的 Domain 设置为 .example.com,允许所有子域名(app1、app2)读取该 Cookie。
      通过统一的加密密钥生成和验证 Cookie,确保安全性。

    3. 认证流程
      用户在任一子应用登录,认证成功后生成加密的 Cookie。
      访问其他子应用时,浏览器自动携带该 Cookie,应用服务器解密后验证用户身份。

    4. 环境配置
      域名与 HTTPS
      确保应用部署在子域名下(如 app1.example.com、app2.example.com)。
      必须启用 HTTPS(防止 Cookie 被窃取),可通过自签名证书或 Let’s Encrypt 实现。

    5. 开发环境
      修改本地 hosts 文件模拟子域名:
      在这里插入图片描述

    6. 代码实现(ASP.NET Core)

1. 配置共享 Cookie
在 Startup.cs 中统一配置 Cookie 认证:
public void ConfigureServices(IServiceCollection services)
{// 配置数据保护共享密钥(所有应用使用相同的密钥)services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"\\shared-network-path\keys")).SetApplicationName("ExampleSSO");services.AddAuthentication("SharedCookie").AddCookie("SharedCookie", options =>{options.Cookie.Name = "SSOAuthCookie";options.Cookie.Domain = ".example.com"; // 关键:允许子域名共享options.Cookie.SecurePolicy = CookieSecurePolicy.Always; // 强制 HTTPSoptions.Cookie.HttpOnly = true; // 防止 XSSoptions.Cookie.SameSite = SameSiteMode.Lax; // 平衡安全与跨站请求options.LoginPath = "/Account/Login"; // 登录页路径options.AccessDeniedPath = "/Account/AccessDenied";});services.AddControllersWithViews();
}
      2.  登录逻辑
在 AccountController.cs 中处理登录请求:
public class AccountController : Controller
{[HttpPost]public async Task<IActionResult> Login(string username, string password){// 模拟用户验证if (username == "admin" && password == "123456"){var claims = new List<Claim>{new Claim(ClaimTypes.Name, username),new Claim(ClaimTypes.Role, "User")};var claimsIdentity = new ClaimsIdentity(claims, "SharedCookie");var authProperties = new AuthenticationProperties{IsPersistent = true // 持久化 Cookie(可选)};await HttpContext.SignInAsync("SharedCookie",new ClaimsPrincipal(claimsIdentity),authProperties);return RedirectToAction("Index", "Home");}ModelState.AddModelError(string.Empty, "用户名或密码错误");return View();}[HttpPost]public async Task<IActionResult> Logout(){await HttpContext.SignOutAsync("SharedCookie");return RedirectToAction("Index", "Home");}
}
3. 验证用户身份
在需要授权的控制器或方法上添加 [Authorize] 属性:
[Authorize]
public class HomeController : Controller
{public IActionResult Index(){// 获取当前用户信息var userName = User.Identity.Name;return View();}
}
  1. 测试流程
    访问应用A:打开 https://app1.example.com/Home/Index,由于未登录,自动跳转至 https://app1.example.com/Account/Login。
    输入凭证登录:提交后生成加密的 Cookie(域名为 .example.com)。
    访问应用B:打开 https://app2.example.com/Home/Index,浏览器自动携带 Cookie,应用B解密后直接授权访问。

  2. 关键问题与解决方案
    Cookie 无法共享
    检查 Domain 配置:确保 .example.com 的域名设置正确(注意开头的点)。
    验证 HTTPS:Cookie 的 Secure 属性要求 HTTPS。

  3. 用户会话不一致
    共享数据保护密钥:所有应用需使用相同的密钥存储路径(如网络共享目录或数据库)。

.PersistKeysToFileSystem(new DirectoryInfo(@"\\shared-network-path\keys"))

跨域请求限制
配置 CORS(如涉及 API 调用):

services.AddCors(options =>
{options.AddPolicy("AllowSubdomains", builder =>builder.WithOrigins("https://app1.example.com", "https://app2.example.com").AllowAnyHeader().AllowAnyMethod().AllowCredentials());
});

安全增强建议
定期轮换密钥

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com