【发布时间】:2019-06-02 18:30:03
【问题描述】:
尝试使用身份服务器 4 实现 Swagger 授权。出现错误但不知道我在哪里做错了。
身份服务器设置
public IEnumerable<Client> GetClients()
{
var client = new List<Client>
{
new Client
{
ClientId = ConstantValue.ClientId,
ClientName = ConstantValue.ClientName,
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RequireConsent = false,
RedirectUris = { string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientUrl"], "assets/oidc-login-redirect.html"), string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientUrl"], "assets/silent-redirect.html") },
PostLogoutRedirectUris = { string.Format("{0}?{1}", Configuration["IdentityServerUrls:ClientUrl"] , "postLogout=true") },
AllowedCorsOrigins = { Configuration["IdentityServerUrls: ClientUrl"] },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
ConstantValue.ClientDashApi
},
IdentityTokenLifetime=120,
AccessTokenLifetime=120
},
new Client
{
ClientId = "swaggerui",
ClientName = "Swagger UI",
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RedirectUris =
{
string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientApiUrl"], "swagger/oauth2-redirect.html"),
string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientApiUrl"], "swagger/o2c.html")
},
AllowedCorsOrigins = { Configuration["IdentityServerUrls: ClientApiUrl"] },
AllowedScopes = {ConstantValue.ClientDashApi},
},
};
return client;
}
API 代码
services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("oauth2", new OAuth2Scheme
{
Type = "oauth2",
Flow = "implicit",
AuthorizationUrl = "http://localhost:44305/connect/authorize",
TokenUrl = "http://localhost:44305/connect/token",
Scopes = new Dictionary<string, string>()
{
{ "mero-rental-client-api", "mero-rental-client-api" }
}
});
});
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Mero Rental API V1");
options.OAuthClientId("swaggerui");
options.OAuthAppName("Swagger UI");
});
这是重定向网址
http://localhost:44305/Account/Login?ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fresponse_type%3Dtoken%26client_id%3Dswaggerui%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%253A44333%252Fswagger%252Foauth2-redirect.html%26scope%3Dmero-rental-client-api%26state%3DV2VkIEphbiAwOSAyMDE5IDE0OjUzOjMzIEdNVCsxMTAwIChBdXN0cmFsaWFuIEVhc3Rlcm4gRGF5bGlnaHQgVGltZSk%253D
这是我在控制台中遇到的错误。 在浏览器上。
参考
https://dave.vanherten.ca/2017/03/swagger-identityserver4-part2/
【问题讨论】:
-
请求颁发令牌以访问 Web api 失败,因为未经授权的客户端。检查 api 项目中的客户端详细信息是否正确。
-
我现在可以登录了。但是登录后页面不会重定向到swagger api页面
标签: asp.net-mvc asp.net-core oauth-2.0 swagger identityserver4