我正在使用具有个人用户帐户身份验证的MVC 5 Web Api 2。我已将访问令牌设置为30天。
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(30), // Expiration 30 Days
AllowInsecureHttp = true
};
但我的访问令牌提前到期(我认为它会在9-10小时后到期)。所以我想调用jquery ajax来刷新令牌“/ Token”。我提到RFC6749从这个艺术中我创造了ajax: -
var data = "refresh_token="+refresh_token;
data = data + "&grant_type=refresh_token"
$.ajax({
type: 'post',
url: "/Token",
data: data,
Content-Type: application/x-www-form-urlencoded
success: function (data) {
saveAccessToken(data);
}
});
我在“ApplicationOAuthProvider.cs”中也创建了GrantRefreshToken
public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
{
return base.GrantRefreshToken(context);
}
调用ajax时,我收到了invalid_type无效的错误。不知道为什么?请帮忙。提前致谢
答案 0 :(得分:0)
通过引用链接Refresh Token解决了这个问题。 添加了课程
public class ApplicationRefreshTokenProvider : AuthenticationTokenProvider
{
public override void Create(AuthenticationTokenCreateContext context)
{
// Expiration time in seconds
int expire = 100*60;
context.Ticket.Properties.ExpiresUtc = new DateTimeOffset(DateTime.Now.AddSeconds(expire));
context.SetToken(context.SerializeTicket());
}
public override void Receive(AuthenticationTokenReceiveContext context)
{
context.DeserializeTicket(context.Token);
}
}
并将其添加到Startup.Auth.cs
中RefreshTokenProvider = new ApplicationRefreshTokenProvider()
Ajax Call
$.ajax({
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
},
type: 'post',
url: "http://MyLink/Token",
dataType: 'application/x-www-form-urlencoded',
data: "grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA..",
success: function (data) {
saveAccessToken(data);
}
});