简体   繁体   中英

IdentityServer3 with aspnet core client

I have setup IdentityServer3 and can successfully authenticate using a username and password stored on an aspnetIdentity database. The problem is on the client MVC application side. After receiving the authorization code from identityserver application it then throws the following exception:

An unhandled exception occurred while processing the request.

InvalidOperationException: No authentication handler is configured to handle the scheme: cookies

My Startup.cs looks like this:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseDatabaseErrorPage();
    app.UseBrowserLink();
}
else
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseApplicationInsightsExceptionTelemetry();
app.UseStaticFiles();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
    AuthenticationScheme = "Cookies",
    AutomaticAuthenticate = true,
    AutomaticChallenge = true

});
var secret = Configuration["Secrets:SharedSecret"];//.ToSha256();
var connectOptions = new OpenIdConnectOptions
{
    AutomaticChallenge = true,
    AutomaticAuthenticate=true,
    AuthenticationScheme = "oidc",
    SignInScheme = "cookies",
    Authority = "http://localhost:4889/core/",
    PostLogoutRedirectUri = "http://localhost:5059/",
    CallbackPath = "/home/index",
    ClientSecret = secret,
    RequireHttpsMetadata = false,
    ClientId = "communicator",
    DisplayName = "Communicator",
    ResponseType = "code id_token",
    GetClaimsFromUserInfoEndpoint = true,
    SaveTokens = true,
    Events = new OpenIdConnectEvents()
    {
        OnUserInformationReceived = async y =>
        {

            var identity = y.Ticket.Principal.Identity as ClaimsIdentity;
            var subject = identity.Claims.FirstOrDefault(z => z.Type == "sub");
            // Do something with subject like lookup in local users DB.
            var newIdentity = new ClaimsIdentity( y.Ticket.AuthenticationScheme,"given_name","role");
            // Do some stuff to `newIdentity` like adding claims.
            // Create a new ticket with `newIdentity`.
                //Ticket = new Ticket(new ClaimsPrincipal(newIdentity),
                //y.Ticket.Properties,
                //y.Ticket.AuthenticationScheme);

            await Task.FromResult(0);
        },
        OnAuthorizationCodeReceived= async c=>
        {
            var identity = c.Ticket.Principal.Identity as ClaimsIdentity;
            var subject =   identity.Claims.FirstOrDefault(z => z.Type == "sub");
            await Task.FromResult(0);
        }

    }
};
connectOptions.Scope.Clear();
connectOptions.Scope.Add("openid");
connectOptions.Scope.Add("profile");
connectOptions.Scope.Add("roles");
connectOptions.Scope.Add("smsapi");
app.UseOpenIdConnectAuthentication(connectOptions);

看起来您的配置中存在区分大小写的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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