2016-12-15 127 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