2016-12-08 90 views
2

我在过去的一年中使用IdentityServer3和多个应用程序。所有这些应用程序都运行在.Net Framework 4.5上。现在我已经创建了ASP.Net Core MVC(1.1)应用程序,并且我想使用我的IdentityServer来保护此应用程序。但我正在努力使其工作。我得到401未经授权的错误。401未经IdentityServer3授权在ASP.Net Core 1.1 MVC应用程序

这里是我使用的配置:

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationScheme = "Cookies", 
    AutomaticAuthenticate = true, 
    AutomaticChallenge = true 
}); 

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); 

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions 
{ 
    AuthenticationScheme = "oidc", 
    SignInScheme = "Cookies", 

    Authority = "<IdentityServer URL>", 
    RequireHttpsMetadata = false, 

    ClientId = "clientid", 

    ResponseType = "token id_token", 
    Scope = { "openid","email"}, 

    SaveTokens = true, 

    PostLogoutRedirectUri = "http://localhost:29995/", 

    Scope = { "openid, email" }, 
    ResponseType = "id_token token", 

    Events = new OpenIdConnectEvents() 
    { 
     OnTokenValidated = (context) => 
     { 
      //my code 
      return Task.FromResult(0); 
     } 
    } 
}); 

当我试着打我收到401未授权安全网页。它甚至没有进入IdentityServer的登录页面。

地址,它是试图击中

https://<IdentityServerUrl>/connect/authorize?client_id=APITestweb&redirect_uri=http://localhost:29995/signin-oidc&response_type=code id_token&scope=openid profile email&response_mode=form_post&nonce=<data>&state=<data> 

客户端ID和URL的正确配置。我试着创建另一个ASP.Net MVC应用程序(而不是ASP.Net Core),并且工作得很好,具有相同的配置信息。

请帮忙。

[编辑]添加日志信息

[Debug] Executing action "WebClient.Controllers.HomeController.About (WebClient)" 
[Information] Authorization failed for user: null. 
[Warning] Authorization failed for the request at filter '"Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter"'. 
[Information] Executing ChallengeResult with authentication schemes ([]). 
[Debug] AuthenticationScheme: "Cookies" was not authenticated. 
[Verbose] Entering "Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler"'s HandleUnauthorizedAsync. 
[Verbose] Using properties.RedirectUri for 'local redirect' post authentication: '"http://localhost:29995/Home/About"'. 
[Debug] Reading data from file '"C:\path\DataProtection-Keys\key-2a848b38-3cf4-4c57-b8ef-4c0b56c83993.xml"'. 
[Debug] Found key {2a848b38-3cf4-4c57-b8ef-4c0b56c83993}. 
[Debug] Considering key {2a848b38-3cf4-4c57-b8ef-4c0b56c83993} with expiration date 2017-03-08 21:08:54Z as default key. 
[Debug] Decrypting secret element using Windows DPAPI. 
[Debug] Opening CNG algorithm '"AES"' from provider 'null' with chaining mode CBC. 
[Debug] Opening CNG algorithm '"SHA256"' from provider 'null' with HMAC. 
[Debug] Using key {2a848b38-3cf4-4c57-b8ef-4c0b56c83993} as the default key. 
[Verbose] Performing protect operation to key {2a848b38-3cf4-4c57-b8ef-4c0b56c83993} with purposes "('project path', 'Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectMiddleware', 'System.String', 'oidc', 'v1')". 
[Verbose] Performing protect operation to key {2a848b38-3cf4-4c57-b8ef-4c0b56c83993} with purposes "('project path', 'Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectMiddleware', 'System.String', 'oidc', 'v1')". 
[Information] AuthenticationScheme: "oidc" was challenged. 
[Information] Request finished in 1301.6524ms 401 
[Debug] Connection id ""0HL10CR7G2G0J"" completed keep alive response. 
+0

您是否已将日志记录添加到1.1项目中?日志记录说什么? – Lutando

+0

@Lutando谢谢。我已将日志信息添加到问题中。 – user3731783

+0

什么版本的OIDC mw? –

回答

3

如果您使用Microsoft.AspNetCore.Authentication.OpenIdConnect 1.1.0,目前旗下有一个错误(或重大更改,因为有人可能称呼它)。

对我来说,1.0.0包仍然有效,所以请检查降级是否有帮助。

Link to GitHub issue on change introduced in 1.1.0 and why it was breaking

+0

你在现场。非常感谢。对于其他有类似问题的人,我也必须降级Microsoft.AspNetCore.Authentication.Cookies。之后,我必须将singin-oidc路径添加到允许的重定向uri中,而这完全是.Net Framework所不需要的。 – user3731783

相关问题