【发布时间】:2014-08-05 20:51:49
【问题描述】:
我在我的 web api 授权中使用 OWIN/Katana 中间件。
流动。
我正在向发出请求的客户端发出 acess_token 和 refresh_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_id 和 client_secret。好像是在他们登录 API 的开发者页面时生成的。
现在,如何在我的项目中实现它?我正在考虑覆盖令牌创建,并使令牌哈希基于ClientId 和ClientSecret。
我正在使用最新 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