我想分享什麼我必須做這項工作,我從其他不同的職位收集答案;你可以在這個答案的最後找到鏈接。
如果有人有任何意見或想法,請與我們分享。
使用刷新令牌生成本地訪問令牌用戶註冊爲響應後。
作爲Peter Hedberg答案;我們需要OAuthOptions Puplic和靜在啓動類爲:
public static OAuthAuthorizationServerOptions OAuthServerOptions { get; private set; }
然後,我創建輔助類來生成本地訪問令牌和刷新
public async Task<JObject> GenerateLocalAccessToken(ApplicationUser user)
{
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager,
OAuthDefaults.AuthenticationType);
AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName);
//Create the ticket then the access token
var ticket = new AuthenticationTicket(oAuthIdentity, properties);
ticket.Properties.IssuedUtc = DateTime.UtcNow;
ticket.Properties.ExpiresUtc = DateTime.UtcNow.Add(Startup.OAuthServerOptions.AccessTokenExpireTimeSpan);
var accessToken = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket);
//Create refresh token
Microsoft.Owin.Security.Infrastructure.AuthenticationTokenCreateContext context =
new Microsoft.Owin.Security.Infrastructure.AuthenticationTokenCreateContext(
Request.GetOwinContext(),
Startup.OAuthOptions.AccessTokenFormat, ticket);
await Startup.OAuthOptions.RefreshTokenProvider.CreateAsync(context);
properties.Dictionary.Add("refresh_token", context.Token);
//create the Token Response
JObject tokenResponse = new JObject(
new JProperty("access_token", accessToken),
new JProperty("token_type", "bearer"),
new JProperty("expires_in", Startup.OAuthServerOptions.AccessTokenExpireTimeSpan.TotalSeconds.ToString()),
new JProperty("refresh_token", context.Token),
new JProperty("userName", user.UserName),
new JProperty(".issued", ticket.Properties.IssuedUtc.ToString()),
new JProperty(".expires", ticket.Properties.ExpiresUtc.ToString())
);
return tokenResponse;
}
有使用基本context.SerializeTicket問題在SimpleRefreshTokenProvider CreateAsync方法中。從Bit Of Technology
消息似乎在ReceiveAsync方法中,context.DeserializeTicket不是 在外部登錄的情況下在所有的返回認證券。 當我看到context.Ticket屬性後,它稱爲null。 將其與本地登錄流程進行比較,DeserializeTicket方法 將context.Ticket屬性設置爲AuthenticationTicket。所以 現在的奧祕在於DeserializeTicket在 這兩個流程中表現如何。在數據庫中的受保護的票字符串在同一CreateAsync方法創建 ,不同的只是,我稱之爲手動 方法在GenerateLocalAccessTokenResponse,主場迎戰Owin middlware稱之爲正常...而且,無論SerializeTicket或 DeserializeTicket拋出一個錯誤...
因此,您需要使用Microsoft.Owin.Security.DataHandler.Serializer.TicketSerializer來對票證進行搜索和反序列化。這將是這個樣子:中
Microsoft.Owin.Security.DataHandler.Serializer.TicketSerializer serializer
= new Microsoft.Owin.Security.DataHandler.Serializer.TicketSerializer();
token.ProtectedTicket = System.Text.Encoding.Default.GetString(serializer.Serialize(context.Ticket));
代替:
token.ProtectedTicket = context.SerializeTicket();
而對於ReceiveAsync法:
Microsoft.Owin.Security.DataHandler.Serializer.TicketSerializer serializer = new Microsoft.Owin.Security.DataHandler.Serializer.TicketSerializer();
context.SetTicket(serializer.Deserialize(System.Text.Encoding.Default.GetBytes(refreshToken.ProtectedTicket)));
代替:
context.DeserializeTicket(refreshToken.ProtectedTicket);
請參閱本Qestion一d此Answer 謝謝lincx和Giraffe