简体   繁体   English

如何为IdentityServer3设置MVC客户端

[英]How to setup an MVC client for IdentityServer3

I'm trying to use the IdentityServer3 therefore I'm going over the official examples. 我正在尝试使用IdentityServer3,因此将介绍官方示例。 I have created an authorization server which is very simple: 我创建了一个非常简单的授权服务器:

namespace SimpleIdentityServer
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var options = new IdentityServerOptions
            {
                Factory = new IdentityServerServiceFactory()
                                .UseInMemoryClients(Clients.Get())
                                .UseInMemoryScopes(Scopes.Get())
                                .UseInMemoryUsers(Users.Get()),
                RequireSsl = false
            };
            app.UseIdentityServer(options);
        }
    }
}

This is my in memory user: 这是我的内存用户:

new Client
{
    ClientName = "MVC application",
    ClientId = "mvc",
    Enabled = true,
    AccessTokenType = AccessTokenType.Jwt,
    Flow = Flows.Implicit,
    ClientSecrets = new List<Secret>
    {
        new Secret("secret".Sha256())
    },
    AllowedScopes = new List<string>
    {
        "openId",
        "profile"
    },
    RedirectUris = new List<string>
    {
        "http://localhost:12261/"
    }
}

Now, I want to use the aforementioned server to authenticate the users of an MVC application, so I have done this: 现在,我想使用前面提到的服务器对MVC应用程序的用户进行身份验证,所以我这样做了:

    public void Configuration(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Cookies"
        });
        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            Authority = "http://localhost:47945/",
            ClientId = "mvc",
            RedirectUri = "http://localhost:12261/",
            ResponseType = "id_token",
            SignInAsAuthenticationType = "Cookies"
        });
    }

And this is a sample controller action annotated with the Authorize attribute: 这是一个示例控制器动作,带有Authorize属性:

[Authorize]
public ActionResult About()
{
    ViewBag.Message = "Your application description page.";

    return View();
}

But when I go to home/about in my mvc application it shows me 401 error and it seems (from the serilog) that it doesn't even call the authorization server. 但是,当我在mvc应用程序中回家/大约时,它显示401错误,并且(从serilog中)似乎它甚至没有调用授权服务器。

What I think could happen is that your OWIN pipeline is not executed. 我认为可能发生的是您的OWIN管道未执行。 Could you try to put a breakpoint in your Startup class, kill IIS or IIS Express - whichever you're using - and starting again? 您是否可以尝试在Startup类中放置一个断点,杀死IIS或IIS Express(无论您使用的是哪个),然后重新启动?

If this is the case, then the IODC middleware doesn't catch the HTTP 401 response, thus doesn't redirect you to your IdentityServer instance. 如果是这种情况,则IODC中间件不会捕获 HTTP 401响应,因此不会将您重定向到IdentityServer实例。

A possible explanation for this would be that you didn't include the necessary NuGet package that enables OWIN when running an ASP.NET app on IIS. 对此的可能解释是,您没有包括在IIS上运行ASP.NET应用程序时启用OWIN的必要NuGet软件包。 That package is Microsoft.Owin.Host.SystemWeb . 该软件包是Microsoft.Owin.Host.SystemWeb

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

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