【问题标题】:Is token based security in WebAPI2 stateless?WebAPI2 中基于令牌的安全性是无状态的吗?
【发布时间】:2020-02-03 19:18:27
【问题描述】:

我只是想理解下面的代码:

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

        ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);

        if (user == null)
        {
            context.SetError("invalid_grant", "The user name or password is incorrect.");
            return;
        }

        ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
           OAuthDefaults.AuthenticationType);
        ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
            CookieAuthenticationDefaults.AuthenticationType);

        AuthenticationProperties properties = CreateProperties(user.UserName);
        AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
        context.Validated(ticket);
        context.Request.Context.Authentication.SignIn(cookiesIdentity);
    }

基于这一行

context.Request.Context.Authentication.SignIn(cookiesIdentity);

看起来它将每个令牌的声明身份信息(直到令牌过期)保存在服务器(会话)上的 cookie 中的某个位置。它可能是一个字典,键作为令牌,值作为 ClaimsIdentity 的对象。

【问题讨论】:

  • 我认为您误解了代码。 *OAuth 将令牌解析为用户名和密码 * - 不,用户将其凭据(用户名/密码)发送到端点以请求令牌,上面的代码验证凭据并创建令牌。
  • 是的,没错。我误会了。
  • @jps 我现在已经编辑了这个问题。你能帮我更好地理解它吗?

标签: oauth-2.0 asp.net-web-api2 bearer-token


【解决方案1】:
  1. 根据用户名 /password 和密钥创建令牌
  2. 此令牌随每个请求一起传递。
  3. 如果是这样,则无法更改此令牌,然后声明令牌 无效的。
  4. 所以不需要 sessionid,令牌本身就足够了 全部。参考https://jwt.io/。在它的主页上你可以 可视化代币创建和代币可视化

Deepak Mishra 当您从第三方(如 Google)启用 Oauth 时,它会使用带有回调操作/函数的查询字符串发送大部分信息信息

https://accounts.google.com/signin/oauth/oauthchooseaccount?client_id=696554540965-tuss1evt7rbudu8rknmh0vb3bvpi8buv.apps.googleusercontent.com&as=zknK7Evpniq6-shKdvjSPQ&destination=https%3A%2F%2Fwww.foo.com%3A433&approval_state=!ChRpV1JYckwtUTZmaGxNSkk1aVRWXxIfVTdvZEdUU2xsa1VaOEhuU1JuY2dubXItM3JDU0FSYw%E2%88%99AJDr988AAAAAXj0CaAvlDBEoTUtus4huzvphtBXXPAK7&oauthgdpr=1&xsrfsig=ChkAeAh8Tz2ZsIc_iVmqimfC6hrE25szMVwwEg5hcHByb3ZhbF9zdGF0ZRILZGVzdGluYXRpb24SBXNvYWN1Eg9vYXV0aHJpc2t5c2NvcGU&flowName=GeneralOAuthFlow

OAuth 库具有解码对象的内置功能

插入带有 ID 和安全标记(不是 GOOGLE SID)的 AspNetUsers 表

并在浏览器和本地存储中的 cookie 中输入无状态特性

AND“如果我偶然知道你的加密算法.....”

即使你能够知道加密算法(我非常怀疑,因为他们有很深的机械。)/他们的安全密钥(我再次高度怀疑)如果你修改它,它就会被改变或无效...... .

如果您创建自己的库并允许无效令牌,那么您又一次违背了 Oauth 的全部目的

身份验证:当前请求可用的身份验证中间件功能。

    SignIn:
        Add information to the response environment that will cause the appropriate authentication
             middleware to grant a claims - based identity to the recipient of the response.
             The exact mechanism of this may vary. Examples include setting a cookie, to adding
             a fragment on the redirect url, or producing an OAuth2 access code or token response.

    cookiesIdentity:
        Determines which claims are granted to the signed in user.The ClaimsIdentity.AuthenticationType
             property is compared to the middleware's Options.AuthenticationType value to
             determine which claims are granted by which middleware. The recommended use is
             to have a single ClaimsIdentity which has the AuthenticationType matching a specific
             middleware.

【讨论】:

  • 这条线在做什么context.Request.Context.Authentication.SignIn(cookiesIdentity);
猜你喜欢
  • 2018-02-08
  • 1970-01-01
  • 2016-05-15
  • 2011-06-08
  • 1970-01-01
  • 2012-07-06
  • 2015-02-06
  • 2020-01-11
  • 1970-01-01
相关资源
最近更新 更多