簡體   English   中英

如何為IdentityServer3設置MVC客戶端

[英]How to setup an MVC client for IdentityServer3

我正在嘗試使用IdentityServer3,因此將介紹官方示例。 我創建了一個非常簡單的授權服務器:

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中)似乎它甚至沒有調用授權服務器。

我認為可能發生的是您的OWIN管道未執行。 您是否可以嘗試在Startup類中放置一個斷點,殺死IIS或IIS Express(無論您使用的是哪個),然后重新啟動?

如果是這種情況,則IODC中間件不會捕獲 HTTP 401響應,因此不會將您重定向到IdentityServer實例。

對此的可能解釋是,您沒有包括在IIS上運行ASP.NET應用程序時啟用OWIN的必要NuGet軟件包。 該軟件包是Microsoft.Owin.Host.SystemWeb

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM