【发布时间】:2018-07-22 22:57:06
【问题描述】:
有谁知道如何让 MVC 4 客户端应用程序使用 identityserver4 作为身份验证提供者?
我已经尝试过 identityserver3 的示例代码,但没有成功。根据对[Authorize] 操作的请求,它可能会重定向到identityserver4 登录端点并给出未知错误。
据我所知,我无法在 identityserver4 'start-up.cs' 和 MVC 客户端使用 OWIN 的 'startup.cs' 定义客户端。
更新
来自我的 IdentityServer4 应用程序的代码 - MVC 4 客户端定义
// OpenID Connect hybrid flow and client credentials client (MVC)
new Client
{
ClientId = "mvc4",
ClientName = "MVC 4 Client",
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
RequireConsent = false,
ClientSecrets =
{
new Secret("secret".Sha256())
},
RedirectUris = { "http://localhost:53173/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:53173/signout-callback-oidc" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"api1"
},
AllowOfflineAccess = true
}
还有来自我的 MVC 4 应用的“Startup.cs”的代码
public void Configuration(IAppBuilder app)
{
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary<string, string>();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "http://localhost:5000/",
RequireHttpsMetadata = false,
ClientId = "mvc4",
ClientSecret = "secret",
ResponseType = "code id_token",
Scope = "openid profile api1 offline_access",
UseTokenLifetime = false,
SignInAsAuthenticationType = "Cookies",
});
}
更新 2
我将 MVC 4 客户端的 Startup.cs 更改为:
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = "oidc",
SignInAsAuthenticationType = "Cookies",
Authority = "http://localhost:5000",
RequireHttpsMetadata = false,
RedirectUri = "http://localhost:53173/signin-oidc",
ClientId = "mvc4",
ClientSecret = "secret",
ResponseType = "code id_token"
});
它现在显示一个登录页面,登录用户,然后 IdentityServer 进入永无止境的循环:
更新 3
public void Configuration(IAppBuilder app)
{
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary<string, string>();
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = "oidc",
SignInAsAuthenticationType = "Cookies",
Authority = "http://localhost:5000",
RequireHttpsMetadata = false,
RedirectUri = "http://localhost:53173/signin-oidc",
ClientId = "mvc4",
ClientSecret = "secret",
ResponseType = "code id_token",
Scope = "openid profile api1 offline_access",
AuthenticationMode = AuthenticationMode.Active
});
}
按照建议添加了范围,但仍然存在循环;请求在 MVC4 客户端和 IdentityServer4 之间摆动。
更新 4
已解决 - 检查我的答案。
【问题讨论】:
-
能否在 mvc 应用中发布客户端配置(身份服务器应用)和 startup.cs。如果您分享错误详细信息会更好。
-
@ademcaglin 谢谢...我更新了我的代码请求审查
-
两个客户端 ID 应该匹配。将 mvc5 更改为 mvc4 并重试。
-
是这样吗,当我发布代码不起作用时,实际上观察到了这个错误......
-
查看身份服务器生成的日志。
标签: asp.net-mvc openid identityserver4