MVC客户端未重定向到IdentityServer4登录UI

时间:2016-11-17 22:35:42

标签: identityserver4

我正在通过IdentityServer4快速入门工作,并且在使隐式快速入门工作时遇到了问题。我从头开始一切都没有问题(clientcredentials,resourceownerpassword等),直到我试图让MVC客户端使用Implicit流来访问具有Authorize属性的控制器方法。我的所有代码似乎都与GitHub示例中的代码相匹配(具有适当的端口更改等),但是当MVC客户端访问控制器时,它会收到401错误,这是合适的....但它不显示快速入门MVC UI (使用提供的powerscript文件安装到IdentityServer项目中)以便我可以登录。似乎我错过了一些东西。我已经提供了日志记录,输出和我理解的相关代码。如何将失败的MVC尝试连接到登录UI?

我很感激你能提供的任何见解!

端口50488是identityserver .net核心应用程序 端口61022是MVC客户端应用程序

MVC客户端尝试期间IdentityServer的日志记录控制台: IdentityServer4 Log

Visual Studio输出窗口:

  

Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求   启动HTTP / 1.1 GET http://localhost:61022/Home/Contact
  Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:信息:   用户授权失败:(null)。

     

Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:警告:   过滤器请求的授权失败   ' Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'

     

Microsoft.AspNetCore.Mvc.ChallengeResult:信息:正在执行   ChallengeResult与身份验证方案()。

     

' dotnet.exe' (CoreCLR:clrhost):已加载   ' C:\用户\ keith.hurley.nuget \包\ System.Threading.Tasks.Extensions \ 4.3.0 \ lib中\ netstandard1.0 \ System.Threading.Tasks.Extensions.dll&#39 ;.   跳过加载符号。模块已优化并具有调试器选项   ' Just My Code'已启用。

     

' dotnet.exe' (CoreCLR:clrhost):已加载   ' C:\用户\ keith.hurley.nuget \包\ System.Globalization.Extensions \ 4.3.0 \运行时\赢\ lib中\ netstandard1.3 \ System.Globalization.Extensions.dll&#39 ;.   跳过加载符号。模块已优化并具有调试器选项   ' Just My Code'已启用。

     

' dotnet.exe' (CoreCLR:clrhost):已加载   ' C:\用户\ keith.hurley.nuget \包\ Microsoft.AspNetCore.Http.Extensions \ 1.1.0 \ lib中\ netstandard1.3 \ Microsoft.AspNetCore.Http.Extensions.dll&#39 ;.   跳过加载符号。模块已优化并具有调试器选项   ' Just My Code'已启用。

     

' dotnet.exe' (CoreCLR:clrhost):已加载   ' C:\用户\ keith.hurley.nuget \包\ Microsoft.AspNetCore.Diagnostics.Abstractions \ 1.1.0 \ lib中\ netstandard1.0 \ Microsoft.AspNetCore.Diagnostics.Abstractions.dll&#39 ;.   跳过加载符号。模块已优化并具有调试器选项   ' Just My Code'已启用。

     

' dotnet.exe' (CoreCLR:clrhost):已加载   ' C:\用户\ keith.hurley.nuget \包\ System.Security.Principal \ 4.3.0 \ lib中\ netstandard1.0 \ System.Security.Principal.dll&#39 ;.   跳过加载符号。模块已优化并具有调试器选项   ' Just My Code'已启用。

     

' dotnet.exe' (CoreCLR:clrhost):已加载   ' C:\用户\ keith.hurley.nuget \包\ System.Text.RegularExpressions \ 4.3.0 \ lib中\ netstandard1.6 \ System.Text.RegularExpressions.dll&#39 ;.   跳过加载符号。模块已优化并具有调试器选项   ' Just My Code'已启用。

     

' dotnet.exe' (CoreCLR:clrhost):已加载   ' C:\用户\ keith.hurley.nuget \包\ System.Runtime.CompilerServices.Unsafe \ 4.3.0 \ lib中\ netstandard1.0 \ System.Runtime.CompilerServices.Unsafe.dll&#39 ;.   模块是在没有符号的情况下构建的。

     

' dotnet.exe' (CoreCLR:clrhost):已加载   ' C:\用户\ keith.hurley.nuget \包\ System.IO.Compression \ 4.3.0 \运行时\赢\ lib中\ netstandard1.3 \ System.IO.Compression.dll&#39 ;.   跳过加载符号。模块已优化并具有调试器选项   ' Just My Code'已启用。

     

' dotnet.exe' (CoreCLR:clrhost):已加载   ' C:\用户\ keith.hurley.nuget \包\ System.Security.Cryptography.X509Certificates \ 4.3.0 \运行时间\ WIN \ LIB \ netstandard1.6 \ System.Security.Cryptography.X509Certificates.dll&#39 ;。   跳过加载符号。模块已优化并具有调试器选项   ' Just My Code'已启用。

     

' dotnet.exe' (CoreCLR:clrhost):已加载   ' C:\用户\ keith.hurley.nuget \包\ System.Runtime.CompilerServices.Unsafe \ 4.3.0 \ lib中\ netstandard1.0 \ System.Runtime.CompilerServices.Unsafe.dll&#39 ;.   模块是在没有符号的情况下构建的。

     

Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectMiddleware:信息:   AuthenticationScheme:oidc受到挑战。

     

Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求   完成于1121.3286ms 401

IdentityServer Startup.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.IdentityModel.Tokens.Jwt;

namespace Ident4
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddInMemoryPersistedGrants()
                .AddInMemoryScopes(Config.GetScopes())
                .AddInMemoryClients(Config.GetClients())
                .AddInMemoryUsers(Config.GetUsers());

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseIdentityServer();

            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();

            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hello World!");
            //});
        }
    }
}

IdentityServer客户端

new Client
                {
                    ClientId="mvc",
                    ClientName="MVC Client",
                    AllowedGrantTypes=GrantTypes.Implicit,
                    RedirectUris= {"http://localhost:61022/signin-oidc"},
                    PostLogoutRedirectUris = { "http://localhost:61022" },
                    AllowedScopes=
                    {
                        StandardScopes.OpenId.Name,
                        StandardScopes.Profile.Name,
                        "api1"
                    }
                }

MVC_client Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;

namespace MVC_client
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationScheme = "cookies"
            });

            app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
            {
                AuthenticationScheme = "oidc",
                SignInScheme = "cookies",
                Authority = "http://localhost:50488/",
                RequireHttpsMetadata = false,
                ClientId = "mvc",
                SaveTokens = true
            });


            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();

        }
    }
}

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。对我来说这是nuget包问题。

在引导aspnet核心项目期间,它添加了软件包" Microsoft.AspNetCore.Authentication.Cookies"和" Microsoft.AspNetCore.Authentication.OpenIdConnect"版本" 1.1.0"。

将包版本更改回" 1.0。*"适合我。