我有一些与不记名令牌相关的问题。在 欧文 您可以保护门票Protect(ticket)
像这样:
ClaimsIdentity identity = new ClaimsIdentity(Startup.OAuthServerOptions.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
Dictionary<string, string> properties = new Dictionary<string, string>();
properties.Add("UserId", user.Id);
properties.Add("UserName", user.UserName);
properties.Add("Role", "user");
AuthenticationProperties properties = new AuthenticationProperties(properties);
AuthenticationTicket ticket = new AuthenticationTicket(identity, properties);
DateTime currentUtc = DateTime.UtcNow;
DateTime expireUtc = currentUtc.Add(TimeSpan.FromHours(24));
ticket.Properties.IssuedUtc = currentUtc;
ticket.Properties.ExpiresUtc = expireUtc;
string token = OAuthAuthorizationServerOptions.AccessTokenFormat.Protect(ticket)
现在令牌将是这样的:
nqak-9R6U64Owsm_lqn_mJzKc_Djd8iVnIw0EX77v5x2rybhf4m_zg_UnrsoO5BxDZQl0HWrSvvd4efa4ChNSf5rAGhd13aOXZlvwOJOZ5v_9bhRCq8A7tqHyiM6DqVVOyYs3lh2SU-wU1m85HH2IcYDtdTY3ijaKZ_QnP1nsqO5LRnnEL4upbETPW9zqWIZzZBX7_Y2cXi2v0K7WnlRor3gFKIZlU9J-NfidRpWXqq5744NfWWHalYADGS7eUWyuxPJCj9ykHYzaXFksJEXBw
我的问题:
Example:
如果您有令牌字符串,您可以执行以下操作:
AuthenticationTicket ticket = OAuthAuthorizationServerOptions.AccessTokenFormat.Unprotect(token);
现在您可以向其中添加自定义声明。例如,如果有一个role
有价值的主张user
然后您可以修改该声明并添加admin
然后重新编码票证,您将获得具有管理员角色的令牌。
我实际上进行了一些测试,在服务器上编码了一个令牌,然后尝试在另一个系统上修改它,但我不能Unprotect
它。因此我想也许票证是使用最初创建的机器密钥进行加密/解密的。但是如果我尝试Unprotect
它来自它工作的同一台机器。我可以解密并修改它。
有人可以解释一下这个过程吗?