应用程序之间共享身份 - Identity Framework C#.NET
Identity Shared between Applications - Identity Framework C#.NET
我有 2 个应用程序,一个 MVC 5 主网站 (A) 和一个 Web Api 2 从端点 (B) 托管在访问 1 个数据库的两台不同机器上。
这些应用程序应该共享基于表单身份验证的身份验证和授权。推荐的实施方式是什么,一旦在 (A) 上进行身份验证:
- 端点 (B) 开始允许通过装饰有 [Authorize] 属性的控制器的请求
- Http.Context.User.Identity 在 (A) 和 (B) 上不为 NULL
我试过像这样使用身份验证 cookie:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
CookieName = "DefaultCookie",
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/auth/login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>(
validateInterval:TimeSpan.FromMinutes(20),
regenerateIdentity: (manager,user) => user.GenerateUserIdentityAsync(manager))
}
});
但我还没有完全成功。
您可以自己推出,但 IdentityServer4 会做这类事情。粗略地说 1)MVC 认证; 2)MVC获取Id Token; 3) 使用 Id Token 向 Web API 传递请求; 4) Web API 检查token是否有效并获取声明; 5) 如果用户被授权,执行操作; 6) 只要令牌有效,就重复 3 - 5。
我有 2 个应用程序,一个 MVC 5 主网站 (A) 和一个 Web Api 2 从端点 (B) 托管在访问 1 个数据库的两台不同机器上。
这些应用程序应该共享基于表单身份验证的身份验证和授权。推荐的实施方式是什么,一旦在 (A) 上进行身份验证:
- 端点 (B) 开始允许通过装饰有 [Authorize] 属性的控制器的请求
- Http.Context.User.Identity 在 (A) 和 (B) 上不为 NULL
我试过像这样使用身份验证 cookie:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
CookieName = "DefaultCookie",
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/auth/login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>(
validateInterval:TimeSpan.FromMinutes(20),
regenerateIdentity: (manager,user) => user.GenerateUserIdentityAsync(manager))
}
});
但我还没有完全成功。
您可以自己推出,但 IdentityServer4 会做这类事情。粗略地说 1)MVC 认证; 2)MVC获取Id Token; 3) 使用 Id Token 向 Web API 传递请求; 4) Web API 检查token是否有效并获取声明; 5) 如果用户被授权,执行操作; 6) 只要令牌有效,就重复 3 - 5。