我有一些与 Bearer Token 有关的问题。在 Owin 中,您可以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
我的问题:
这个令牌是如何生成/加密的?
是否有人可以尝试弄乱令牌并向其添加一些自定义声明?
例子:
如果你有令牌字符串,你可以这样做:
AuthenticationTicket ticket = OAuthAuthorizationServerOptions.AccessTokenFormat.Unprotect(token);
现在您可以为其添加自定义声明。例如,如果有一个role
具有价值的声明,user
那么您可以修改该声明并添加admin
然后重新编码票证,您将获得一个具有管理员角色的令牌。
我实际上做了一些测试,在服务器上编码了一个令牌,然后尝试在另一个系统上修改它,但我做不到Unprotect
。因此,我在想票证可能是使用最初创建的机器密钥加密/解密的。但是,如果我尝试Unprotect
从同一台机器上运行它。我可以解密并修改它。
有人可以解释一下这个过程吗?