![](/img/trans.png)
[英]OAuth {"error":"invalid_client"} grant type "client credential" - C# ASP.Net Web API
[英]Identity Server OAuth Resource Owner Password Grant always returns invalid_client
new Client
{
ClientId = "esmifavorito",
ClientName = "esmifavorito-client",
Enabled = true,
ClientSecrets = new List<ClientSecret>
{
new ClientSecret("esmifavorito".Sha256()) //PQ/pIgjXnBfK67kOxGxz9Eykft6CKPkPewR3jUNEkZo=
},
Flow = Flows.ResourceOwner,
//RequireConsent = false,
//AllowRememberConsent = false,
//ClientUri = "http",
RedirectUris = new List<string>
{
"https://localhost:44304",
},
ScopeRestrictions = new List<string>
{
},
AllowedCorsOrigins = new List<string>
{
"https://localhost:44304",
"http://localhost:50655",
"chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm",
"*",
},
PostLogoutRedirectUris = new List<string>
{
"https://localhost:44304",
},
AccessTokenType = AccessTokenType.Jwt,
IdentityTokenLifetime = 3000,
AccessTokenLifetime = 3600,
AuthorizationCodeLifetime = 300
}
我已經注冊了我的客戶端,它可以使用隱式流程,但我需要實現一個登錄表單,所以我正在嘗試資源所有者密碼憑據授予。 我正在 Chrome 中使用 Postman 向端點發出請求(這就是我將 chrome 擴展添加到 CORS 的原因,只是為了看看這是否是錯誤......)
我已經嘗試了很多請求(使用 https)
POST /connect/token HTTP/1.1
Host: localhost:44302
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=test&password=testuser&client_id=esmifavorito
——
POST /connect/token HTTP/1.1
Host: localhost:44302
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=test&password=testuser&client_id=esmifavorito&client_secret=PQ%2FpIgjXnBfK67kOxGxz9Eykft6CKPkPewR3jUNEkZo%3D
——
POST /connect/token HTTP/1.1
Host: localhost:44302
Authorization: Basic ZXNtaWZhdm9yaXRvOlBRL3BJZ2pYbkJmSzY3a094R3h6OUV5a2Z0NkNLUGtQZXdSM2pVTkVrWm89
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=test&password=testuser
那些應該有效,但我總是收到 invalid_client
錯誤日志是空的,不知道是不是我把tracer注冊做對了
LogProvider.SetCurrentLogProvider(new DiagnosticsTraceLogProvider());
app.UseIdentityServer(new IdentityServerOptions
{
LoggingOptions = new LoggingOptions {
IncludeSensitiveDataInLogs = true,
WebApiDiagnosticsIsVerbose = true,
EnableWebApiDiagnostics = true,
//EnableHttpLogging = true
},
SiteName = "Thinktecture IdentityServer3 - UserService-AspNetIdentity",
SigningCertificate = Certificate.Get(string.Format(@"{0}\bin\IdentityServer\IdentityServerEMFDev.pfx", AppDomain.CurrentDomain.BaseDirectory), "KG0yM0At"),
Factory = idSvrFactory,
CorsPolicy = CorsPolicy.AllowAll,
AuthenticationOptions = new AuthenticationOptions
{
IdentityProviders = ConfigureAdditionalIdentityProviders,
},
}
);
有了這個在 web.config
<trace autoflush="true"
indentsize="4">
<listeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="Trace.log" />
<remove name="Default" />
</listeners>
</trace>
客戶端數據是正確的,因為我已經使用隱式流成功登錄。 我錯過了什么? 這讓我很緊張,我正在閱讀 OAuth RFC,但我不明白為什么這不起作用。
我嘗試了新版本的Postman(我不知道其編號,但是現在它作為chrome應用程序在桌面上運行),我復制了舊Postman版本中的值,現在一切正常。
POST /connect/token HTTP/1.1
Host: localhost:44302
Authorization: Basic ZXNtaWZhdm9yaXRvOmVzbWlmYXZvcml0bw==
Cache-Control: no-cache
Postman-Token: fc4acc63-29f2-6a37-b92c-b62034b13c29
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=test&password=testuser&scope=write
這是結果請求。 在Postman 1中,我有同樣的事情(不包括Postman-Token
),它給了我invalid_client。 我什至使用了具有相似結果的類似Firefox工具。 我不知道這怎么可能。 可能是chrome-extension://
嗎?
我要回答自己,但如果有人知道這里發生了什么,我將永遠感激不已。
看來這篇文章必須在標頭中發送授權
https://github.com/IdentityServer/IdentityServer3/issues/1520
就我而言,我遇到了同樣的問題,我注意到這是由於我的 HttpClient 設置了“自定義”授權標頭。
如果使用 IdentityModel 請求資源所有者密碼令牌,請注意 Authorization 標頭必須包含 base64 中的client_id:client_secret
。
在我的例子中,我設置了一個不同的授權標頭,雖然正文值是正確的,但IResourceOwnerPasswordValidator
甚至沒有嘗試驗證請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.