OAuthMiddleware:必须提供“AuthenticationScheme”选项

时间:2017-05-05 09:02:39

标签: asp.net-core

大家好!

我正在尝试使用ASP.NET Core实现OpenIDDict以保护我的Web API。但是,在我按照OpenIDDict github页面中的说明操作后,我在浏览器中遇到以下错误:

  

ArgumentException:必须提供“AuthenticationScheme”选项。   Microsoft.AspNetCore.Authentication.OAuth.OAuthMiddleware..ctor(RequestDelegate next,IDataProtectionProvider dataProtectionProvider,ILoggerFactory loggerFactory,UrlEncoder encoder,IOptions sharedOptions,IOptions options)

开发环境和软件版本:

  1. Visual Studio 2017
  2. DB:带有.NET核心标识数据库的SQL Server
  3. .NET Core版本1.1.0
  4. OpenIDDict版本:1.0.0-beta2-0612
  5. 你们对这个错误有什么看法吗?

    我的AuthorizationController.cs文件:

    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Security.Claims;
    using System.Threading.Tasks;
    using AspNet.Security.OpenIdConnect.Extensions;
    using AspNet.Security.OpenIdConnect.Primitives;
    using AspNet.Security.OpenIdConnect.Server;
    using AuthorizationServer.Models;
    using Microsoft.AspNetCore.Authentication;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Http.Authentication;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Options;
    using OpenIddict.Core;
    
    namespace AuthorizationServer.Controllers
    {
        public class AuthorizationController : Controller
        {
            private readonly IOptions<IdentityOptions> _identityOptions;
            private readonly SignInManager<ApplicationUser> _signInManager;
            private readonly UserManager<ApplicationUser> _userManager;
    
            public AuthorizationController(
                IOptions<IdentityOptions> identityOptions,
                SignInManager<ApplicationUser> signInManager,
                UserManager<ApplicationUser> userManager)
            {
                _identityOptions = identityOptions;
                _signInManager = signInManager;
                _userManager = userManager;
            }
    
            [HttpPost("~/connect/token"), Produces("application/json")]
            public async Task<IActionResult> Exchange(OpenIdConnectRequest request)
            {
                Debug.Assert(request.IsTokenRequest(),
                    "The OpenIddict binder for ASP.NET Core MVC is not registered. " +
                    "Make sure services.AddOpenIddict().AddMvcBinders() is correctly called.");
    
                if (request.IsPasswordGrantType())
                {
                    var user = await _userManager.FindByNameAsync(request.Username);
                    if (user == null)
                    {
                        return BadRequest(new OpenIdConnectResponse
                        {
                            Error = OpenIdConnectConstants.Errors.InvalidGrant,
                            ErrorDescription = "The username/password couple is invalid."
                        });
                    }
    
                    // Ensure the user is allowed to sign in.
                    if (!await _signInManager.CanSignInAsync(user))
                    {
                        return BadRequest(new OpenIdConnectResponse
                        {
                            Error = OpenIdConnectConstants.Errors.InvalidGrant,
                            ErrorDescription = "The specified user is not allowed to sign in."
                        });
                    }
    
                    // Reject the token request if two-factor authentication has been enabled by the user.
                    if (_userManager.SupportsUserTwoFactor && await _userManager.GetTwoFactorEnabledAsync(user))
                    {
                        return BadRequest(new OpenIdConnectResponse
                        {
                            Error = OpenIdConnectConstants.Errors.InvalidGrant,
                            ErrorDescription = "The specified user is not allowed to sign in."
                        });
                    }
    
                    // Ensure the user is not already locked out.
                    if (_userManager.SupportsUserLockout && await _userManager.IsLockedOutAsync(user))
                    {
                        return BadRequest(new OpenIdConnectResponse
                        {
                            Error = OpenIdConnectConstants.Errors.InvalidGrant,
                            ErrorDescription = "The username/password couple is invalid."
                        });
                    }
    
                    // Ensure the password is valid.
                    if (!await _userManager.CheckPasswordAsync(user, request.Password))
                    {
                        if (_userManager.SupportsUserLockout)
                        {
                            await _userManager.AccessFailedAsync(user);
                        }
    
                        return BadRequest(new OpenIdConnectResponse
                        {
                            Error = OpenIdConnectConstants.Errors.InvalidGrant,
                            ErrorDescription = "The username/password couple is invalid."
                        });
                    }
    
                    if (_userManager.SupportsUserLockout)
                    {
                        await _userManager.ResetAccessFailedCountAsync(user);
                    }
    
                    // Create a new authentication ticket.
                    var ticket = await CreateTicketAsync(request, user);
    
                    return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);
                }
    
                return BadRequest(new OpenIdConnectResponse
                {
                    Error = OpenIdConnectConstants.Errors.UnsupportedGrantType,
                    ErrorDescription = "The specified grant type is not supported."
                });
            }
    
            private async Task<AuthenticationTicket> CreateTicketAsync(OpenIdConnectRequest request, ApplicationUser user)
            {
                // Create a new ClaimsPrincipal containing the claims that
                // will be used to create an id_token, a token or a code.
                var principal = await _signInManager.CreateUserPrincipalAsync(user);
    
                // Create a new authentication ticket holding the user identity.
                var ticket = new AuthenticationTicket(principal,
                    new AuthenticationProperties(),
                    OpenIdConnectServerDefaults.AuthenticationScheme);
    
                // Set the list of scopes granted to the client application.
                ticket.SetScopes(new[]
                {
                    OpenIdConnectConstants.Scopes.OpenId,
                    OpenIdConnectConstants.Scopes.Email,
                    OpenIdConnectConstants.Scopes.Profile,
                    OpenIddictConstants.Scopes.Roles
                }.Intersect(request.GetScopes()));
    
                ticket.SetResources("resource-server");
    
                // Note: by default, claims are NOT automatically included in the access and identity tokens.
                // To allow OpenIddict to serialize them, you must attach them a destination, that specifies
                // whether they should be included in access tokens, in identity tokens or in both.
    
                foreach (var claim in ticket.Principal.Claims)
                {
                    // Never include the security stamp in the access and identity tokens, as it's a secret value.
                    if (claim.Type == _identityOptions.Value.ClaimsIdentity.SecurityStampClaimType)
                    {
                        continue;
                    }
    
                    var destinations = new List<string>
                    {
                        OpenIdConnectConstants.Destinations.AccessToken
                    };
    
                    // Only add the iterated claim to the id_token if the corresponding scope was granted to the client application.
                    // The other claims will only be added to the access_token, which is encrypted when using the default format.
                    if ((claim.Type == OpenIdConnectConstants.Claims.Name && ticket.HasScope(OpenIdConnectConstants.Scopes.Profile)) ||
                        (claim.Type == OpenIdConnectConstants.Claims.Email && ticket.HasScope(OpenIdConnectConstants.Scopes.Email)) ||
                        (claim.Type == OpenIdConnectConstants.Claims.Role && ticket.HasScope(OpenIddictConstants.Claims.Roles)))
                    {
                        destinations.Add(OpenIdConnectConstants.Destinations.IdentityToken);
                    }
    
                    claim.SetDestinations(destinations);
                }
    
                return ticket;
            }
        }
    }
    

0 个答案:

没有答案