【发布时间】:2019-02-28 05:08:31
【问题描述】:
我们的解决方案有 3 个项目。我们称它们为 A、B 和 IdentityServer。 每个项目都可以作为单独的服务工作。 项目 A 和 B 是 ASP.Net MVC 项目 IdentityServer 是 .Net Core(它使用 IdentityServer4)
前端连接到项目 A。 数据库连接到项目 B。 项目 A 的控制器调用项目 B 的控制器的方法。 而且效果很好。
我们为项目 A 添加了授权,配置了 IdentityServer,一切正常。 当前端从项目A控制器中调用methode时:
[授权] 公共任务方法(...) {}
它工作正常。它将用户重定向到 IdentityServer,请求登录并授权他。
但我们也必须保护项目 B。 当我将 [Authorize] pragma 添加到项目 B 的方法时,当项目 A 的控制器尝试调用该方法时,它会失败。
[Authorize]
public Task<ActionResult> MethodefromServiceA()
{
//do something
//calling methode from service B and sending User as ClaimsPrincipal
}
[Authorize]
public Task<ActionResult> MethodefromServiceB()
{
//it fails here - don't enter to this line (it enter's here when I remove [Authorise] pragma
}
我们不确切知道如何配置 IdentityServer4 和项目 B,当项目 B 控制器从项目 A 获取声明和令牌时,它应该以某种方式连接 IdentityServer 并授权令牌(但不是用户,因为他已经被授权并且我们有令牌)。
在 IdentityServer 的 Configuration.cs 中,我们有这样的内容:
new Client {
ClientId = "A",
ClientName = "A",
AllowedGrantTypes = GrantTypes.Implicit,
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"role",
"customAPI.write",
"customAPI.read"
},
RedirectUris = new List<string> {"http://localhost:64898"},
PostLogoutRedirectUris = new List<string> { "http://localhost:64898/home/welcome" }
},
new Client {
ClientId = "B",
ClientName = "B",
AllowedGrantTypes = GrantTypes.Implicit,
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"role",
"customAPI.write",
"customAPI.read"
},
RedirectUris = new List<string> {"http://localhost:57028"},
PostLogoutRedirectUris = new List<string> { "http://localhost:57028" }
},
在项目 A 的启动中:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = "A",
Authority = "https://localhost:44350/",
RedirectUri = "http://localhost:64898",
ResponseType = "id_token",
Scope = "openid email",
UseTokenLifetime = false,
SignInAsAuthenticationType = "Cookies",
PostLogoutRedirectUri = "http://localhost:64898/home/welcome",
});
我尝试添加类似于项目 B,但它没有改变任何东西。
如果我能找到一些如何使用 IdentityServer4 进行多项目授权的示例,我会很高兴。如何在每个项目中授权令牌等... [Authorize] pragma 是否解决了这样的情况,还是必须以不同的方式或手动完成?
感谢您的帮助。 对不起我的英语
【问题讨论】:
-
我认为您可以改进您的问题,添加以下代码:“来自项目 A 的控制器调用来自项目 B 控制器的方法”,因为您只放置了 cmets 并添加了以下代码中使用的代码:“我尝试添加类似于项目 B”,因为您只放置了项目 A 中的启动代码。
标签: c# .net oauth-2.0 identityserver4