【问题标题】:Web API Authorization with ClientId and ClientSecret使用 ClientId 和 ClientSecret 进行 Web API 授权
【发布时间】:2014-08-05 20:51:49
【问题描述】:

我在我的 web api 授权中使用 OWIN/Katana 中间件。

流动。

我正在向发出请求的客户端发出 acess_tokenrefresh_token

access_token 的使用寿命很短,而refresh_token 的有效期很长。

像往常一样,如果 access_token 过期,它将使用 refresh_token 请求另一个 access_token。

现在,我的问题。由于我的 refresh_token 寿命长,看起来它违背了短暂的 access_token 的目的。假设如果 refresh_token 被泄露,黑客仍然可以获得 access_token,对吧?

我查看了 google 和 microsoft 的 OAuth 实现,似乎除了 refresh_token 之外,他们还需要提供这个附加参数。这是 client_idclient_secret。好像是在他们登录 API 的开发者页面时生成的。

现在,如何在我的项目中实现它?我正在考虑覆盖令牌创建,并使令牌哈希基于ClientIdClientSecret

我正在使用最新 Web api 的基本 OWIN/Katana 身份验证,我不打算使用其他授权服务器,如 Thinktecture。我只想使用 ASP.NET Web API 2 默认提供的基本功能

Startup.OAuth.cs

public partial class Startup
{
   static Startup()
   {
      PublicClientId = "self";
      UserManagerFactory = () => new UserManager<IdentityUser>(new AppUserStore());
      var tokenExpiry = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ApiTokenExpiry"]);

      OAuthOptions = new OAuthAuthorizationServerOptions
      {
          TokenEndpointPath = new PathString("/Token"),
          Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
          AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
          AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(tokenExpiry),
          AllowInsecureHttp = true,
          RefreshTokenProvider = new AuthenticationTokenProvider
          {
               OnCreate = CreateRefreshToken,
               OnReceive = ReceiveRefreshToken,
          }
      };
   }

   private static void CreateRefreshToken(AuthenticationTokenCreateContext context)
   {
       var tokenExpiry = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ApiTokenExpiry"]);
       var refreshTokenExpiry = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ApiRefreshTokenExpiry"]);

       var refreshTokenProperties = new AuthenticationProperties(context.Ticket.Properties.Dictionary)
       {
           IssuedUtc = context.Ticket.Properties.IssuedUtc,
           ExpiresUtc = DateTime.UtcNow.AddMinutes(tokenExpiry + refreshTokenExpiry) // add 3 minutes to the access token expiry
       };

       var refreshTokenTicket = new AuthenticationTicket(context.Ticket.Identity, refreshTokenProperties);

       OAuthOptions.RefreshTokenFormat.Protect(refreshTokenTicket);
       context.SetToken(context.SerializeTicket());
   }

   private static void ReceiveRefreshToken(AuthenticationTokenReceiveContext context)
   {
       context.DeserializeTicket(context.Token);
   }
}

ApplicationOAuthProvider.cs

public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
{
    private readonly string _publicClientId;
    private readonly Func<UserManager<IdentityUser>> _userManagerFactory;

    public ApplicationOAuthProvider(string publicClientId, Func<UserManager<IdentityUser>> userManagerFactory)
    {
        if (publicClientId == null)
        {
            throw new ArgumentNullException("publicClientId");
        }

        if (userManagerFactory == null)
        {
            throw new ArgumentNullException("userManagerFactory");
        }

        _publicClientId = publicClientId;
        _userManagerFactory = userManagerFactory;
    }

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
         using (UserManager<IdentityUser> userManager = _userManagerFactory())
         {
             IdentityUser 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 userManager.CreateIdentityAsync(user,
                    context.Options.AuthenticationType);
             ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                    CookieAuthenticationDefaults.AuthenticationType);
             AuthenticationProperties properties = CreateProperties(user.UserName);
             AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);

             context.Validated(ticket);
             context.Request.Context.Authentication.SignIn(cookiesIdentity);
         }
    }
}

【问题讨论】:

    标签: asp.net asp.net-web-api asp.net-mvc-5 owin katana


    【解决方案1】:

    总结--

    1. 导航到https://console.developers.google.com/
    2. 选择一个项目。如果您没有,请创建一个。
    3. 在左侧栏的 API'S 和 AUTH 下,选择 CREDENTIALS
    4. 您应该会看到您的 clientID 和 clientSecret。如果您没有看到这些,请单击创建新的客户端 ID 并完成该操作。然后应该会显示您的 clientID 和 clientSecret。

    如果您仍然不确定,请参考此链接:https://developers.google.com/accounts/docs/OAuth2Login#getcredentials

    描述了如何获取clientID和clientSecret。

    【讨论】:

      猜你喜欢
      • 2014-09-03
      • 2020-11-23
      • 2016-12-29
      • 2017-09-01
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      相关资源
      最近更新 更多