Ama*_*ika 1 c# asp.net asp.net-identity identityserver4 asp.net-core-2.0
我正在创建一个示例应用程序,以了解身份服务器 4 身份验证如何与 Asp.net core 2 一起工作。我注意到为不同级别生成了一些 cookie,如所附屏幕截图所示。我的问题是为什么会生成这些 cookie?
下面的语句,我取自 Identity Server 文档。身份服务器配置时
IdentityServer 使用自定义方案(通过常量 IdentityServerConstants.DefaultCookieAuthenticationScheme)在内部调用 AddAuthentication 和 AddCookie,
这里为什么它调用AddCookies身份服务器本身的方法?
此外,当我将 Asp.net 核心 Web 客户端配置为使用身份服务器身份验证时,它也会调用AddCookie()方法。当我尝试对其发表评论时,它会给我一个错误。我有点不清楚这里发生了什么。
身份服务器配置
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddToDoUserStore()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
services.AddAuthentication("MyCookie")
.AddCookie("MyCookie", options =>
{
options.ExpireTimeSpan = new System.TimeSpan(0, 0, 15);
});
Run Code Online (Sandbox Code Playgroud)
网页客户端配置
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.Authority = "https://localhost:44377/";
options.RequireHttpsMetadata = true;
options.ClientId = "ToDoTaskManagmentClient";
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("address");
options.Scope.Add("roles");
options.Scope.Add("usertodoapi");
options.Scope.Add("countries");
options.Scope.Add("subscriptionlevel");
options.Scope.Add("offline_access");
options.ResponseType = "code id_token";
options.SaveTokens = true;
options.ClientSecret = "secret";
options.GetClaimsFromUserInfoEndpoint = true;
options.ClaimActions.Clear();
options.ClaimActions.MapJsonKey("given_name", "given_name");
options.ClaimActions.MapJsonKey("family_name", "family_name");
options.ClaimActions.MapJsonKey("role", "role");
options.ClaimActions.MapJsonKey("country", "country");
options.ClaimActions.MapJsonKey("subscriptionlevel", "subscriptionlevel");
options.Events = new OpenIdConnectEvents()
{
OnTokenValidated = e =>
{
var identity = e.Principal;
var subjectClaim = identity.Claims.FirstOrDefault(z => z.Type == "sub");
var expClaims = identity.Claims.FirstOrDefault(z => z.Type == "exp");
var newClaimsIdentity = new ClaimsIdentity(e.Scheme.Name);
newClaimsIdentity.AddClaim(subjectClaim);
newClaimsIdentity.AddClaim(expClaims);
e.Principal = new ClaimsPrincipal(newClaimsIdentity);
return Task.FromResult(0);
},
OnUserInformationReceived = e =>
{
e.User.Remove("address");
return Task.FromResult(0);
}
};
});
Run Code Online (Sandbox Code Playgroud)
您的 Identity Server 应用程序需要一个身份验证 cookie(和会话 ID cookie),以便前端通道端点(授权、同意、check_session_iframe 和其他可能的)知道用户是否通过身份验证以及会话的当前状态。如果没有这个,它就不知道是谁在调用它。如果 IDS4 检测到传入请求未通过身份验证,它将自动重定向到默认方案的登录 URL - 然后您可以自由地实施任何您喜欢的身份验证流程。
您的客户端应用程序可能需要也可能不需要 cookie,具体取决于架构。传统的服务器端 WebForms 或 MVC 风格的应用程序将需要一个,但使用像 oidc-client-js 这样的库的纯 JS 客户端不需要,并且可以纯粹使用从您的身份服务器获取的访问令牌与后端对话。
IdentityServer 不做任何这些事情。它所做的只是处理低级身份验证/授权并返回声明主体。使用 IdentityServer 的应用程序将设置 cookie。
您在这里所做的本质上是让同一个应用程序托管 IdentityServer 和基于 cookie 身份验证的前端。Cookie 部分用于传统的登录流程 UI,以便应用程序可以识别用户是否经过身份验证,并在经过身份验证时重定向到登录表单或帐户页面或返回到原始应用程序。
该部分可以完全拆分为一个完全不同的应用程序,然后您的 IdentityServer 应用程序将不再需要 cookie 身份验证配置。