2016-12-15 110 views
-1

我正在尝试使用IdentityServer3,因此我将通过官方示例。我已经创建了一个授权服务器,这是非常简单的:如何为IdentityServer3设置MVC客户端

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

这是我记忆用户:

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/" 
    } 
} 

现在,我想使用上述服务器的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" 
     }); 
    } 

这是与Authorize属性标注的样本控制器动作:

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

    return View(); 
} 

但是,当我回家/关于我的mvc应用程序时,它显示我401错误,它似乎(从serilog)它甚至不会调用授权服务器。

+0

您是否从Identity Server获取任何错误?假设您的身份服务器作为默认运行并且绑定到/ core /,您的客户端中的Authority应该是http:// localhost:47945/core /。另外,您可能需要告诉客户端网站允许使用外部Cookie: app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); –

+0

@NickBork实际上,我已经在自托管的控制台应用程序上实现了Identity Server:WebApp.Start (“http:// localhost:47945”)'。无论如何,我没有得到任何错误。而且,我还没有添加任何映射(在服务器的启动配置方法中)。 – mok

+0

@NickBork'app.UseExternalSignInCookie(DefaultAuthenticationTypes.Exter nalCookie); '没有改变任何东西。 – mok

回答

3

我认为可能发生的事情是您的OWIN管道未执行。你可以尝试在你的Startup类中放置一个断点,杀死IIS或IIS Express - 无论你正在使用什么 - 然后重新开始?

如果是这样的话,那么IODC中间件不会捕获HTTP 401响应,因此不会将您重定向到您的IdentityServer实例。

对此的一种可能的解释是,您没有包含在IIS上运行ASP.NET应用程序时启用OWIN所需的NuGet包。该包是Microsoft.Owin.Host.SystemWeb