简体   繁体   English

401未经ASP.Net Core 1.1 MVC应用程序中IdentityServer3的授权

[英]401 Unauthorized with IdentityServer3 In ASP.Net Core 1.1 MVC Application

I am using IdentityServer3 with multiple application for the past year. 在过去的一年中,我将IdentityServer3与多个应用程序一起使用。 All those applications are running on .Net Framework 4.5. 所有这些应用程序都在.Net Framework 4.5上运行。 Now I have created ASP.Net Core MVC (1.1) application and I wanted to use my IdentityServer for securing this application. 现在,我已经创建了ASP.Net Core MVC(1.1)应用程序,并且我想使用我的IdentityServer保护该应用程序的安全。 But I am struggling to make it work. 但是我正在努力使它起作用。 I am getting 401 Unauthorized error. 我收到401未经授权的错误。

Here is the configuration I am using: 这是我正在使用的配置:

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);
        }
    }
});

When I try to hit a secured page I am getting 401 Unauthorized. 当我尝试访问安全页面时,我得到401未经授权。 Its not even going to IdentityServer's login page. 它甚至都不会转到IdentityServer的登录页面。

Url that it is trying to hit 尝试击中的网址

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>

Client Id and url's configured properly. 客户端ID和url的配置正确。 I tried creating another ASP.Net MVC application (not ASP.Net Core) and that works just fine with same configuration information. 我尝试创建另一个ASP.Net MVC应用程序(不是ASP.Net Core),并且在使用相同配置信息的情况下也可以正常工作。

Please help. 请帮忙。

[Edit] Adding Logging Information [编辑]添加日志记录信息

[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.

If you're using Microsoft.AspNetCore.Authentication.OpenIdConnect v.1.1.0, there's currently a bug (or breaking change, as one might call it). 如果您使用的是Microsoft.AspNetCore.Authentication.OpenIdConnect v.1.1.0,则当前存在一个错误(或可能称为更改的重大更改)。

For me, the 1.0.0 package is still working, so check if a downgrade helps out. 对我来说,1.0.0软件包仍在工作,因此请检查降级是否有帮助。

Link to GitHub issue on change introduced in 1.1.0 and why it was breaking 链接到1.1.0中引入的有关变更的GitHub问题以及其为何被打破

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM