简体   繁体   中英

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

I am using IdentityServer3 with multiple application for the past year. All those applications are running on .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. But I am struggling to make it work. I am getting 401 Unauthorized error.

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. Its not even going to IdentityServer's login page.

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. I tried creating another ASP.Net MVC application (not ASP.Net Core) and that works just fine with same configuration information.

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

For me, the 1.0.0 package is still working, so check if a downgrade helps out.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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