无法使用MS Oauth2对ASP.NET核心使用刷新令牌

时间:2017-05-16 21:39:19

标签: c# asp.net-core oauth-2.0 refresh-token

我已经回顾了很多问题,但还没有解决这个问题。 对于身份验证,包含Microsoft.AspNetCore.Authentication.MicrosoftAccount。身份验证和授权工作正常。除了令牌刷新之外,图书馆会自动化所有内容,这似乎很尴尬。

对于令牌刷新,包括Array[T]

offline_access

令牌保存在app.UseMicrosoftAccountAuthentication(new MicrosoftAccountOptions { ClientId = Startup.Config["Data:MSAppId"], ClientSecret = Startup.Config["Data:MSAppSecret"], CallbackPath = new PathString("/signin-microsoft-token"), AuthorizationEndpoint = MicrosoftAccountDefaults.AuthorizationEndpoint, SignInScheme = new IdentityCookieOptions().ExternalCookieAuthenticationScheme, TokenEndpoint = MicrosoftAccountDefaults.TokenEndpoint, Scope = { "openid", "email", "profile", "offline_access", "Contacts.Read", "Mail.ReadWrite", "Mail.Send" }, AutomaticAuthenticate = true, AutomaticChallenge = true, SaveTokens = true }); 表中 AspNetUserTokens table

使用访问令牌,成功返回联系人和电子邮件,并代表用户成功发送电子邮件。但是,在尝试使用刷新令牌时,会返回'错误请求 [400]错误。

AspNetUserTokens

我看过fiddler的请求,看起来是正确的。我在上面使用了类似的代码和纯REST解决方案,并没有遇到任何困难。

我原本以为他们的库会有一个更好的解决方案来刷新令牌,因为它可以毫不费力地进行原始身份验证和授权。有没有更好的方法来解决这个问题?如果没有,为什么请求被拒绝?

修改

删除string clientId = /* ... */ string clientSecret = /* ... */ string access_token = /* ... */ string refresh_token = /* ... */ DateTime expiration_date = /* ... */ const string refreshTokenUrl = "https://login.microsoftonline.com/common/oauth2/token"; const string redirectUri = "http://localhost/"; if (DateTime.Now < (expiration_date - TimeSpan.FromMinutes(10))) return access_token; HttpClient client = new HttpClient(); string parameters = $"grant_type=refresh_token&refresh_token={ WebUtility.UrlEncode(refresh_token) }&client_id={ WebUtility.UrlEncode(clientId) }&client_secret={ WebUtility.UrlEncode(clientSecret) }&redirect_uri={ WebUtility.UrlEncode(redirectUri) }"; var contentBody = new StringContent(parameters, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await client.PostAsync(refreshTokenUrl, contentBody); if (!response.IsSuccessStatusCode) { // process 400 - bad request error ... } 会导致相同的错误响应。通过fiddler深入了解,错误响应(JSON)的主体从以下内容开始:

redirect_uri

但我确信整个refresh_token已正确保存并发送,最后一次测试有953个字符。

0 个答案:

没有答案
相关问题