OAuth令牌请求unsupported_grant_type

时间:2019-06-14 09:23:22

标签: c# api oauth odata postman

我正在研究一个自定义API,它将从SQL Server数据库传递信息。我使用OAuth进行授权(尚不完整,现在即使您未被授权,它也应始终发出令牌)。但是,当我尝试请求令牌时,总是收到错误unsupported_grant_type。我不知道为什么,有人可以帮我吗:/顺便说一句,我在Postman中使用GET-URI http://localhost:60998/Token

这是我的OAuth代码:

using Microsoft.Owin.Security.OAuth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Web;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.OAuth;
using System.Threading.Tasks;

namespace ApiStad.Helpers
{
    public class AppOAuthProvider : OAuthAuthorizationServerProvider
    {
        private readonly String _PublicClientID;

        public AppOAuthProvider(String publicClientID)
        {
            _PublicClientID = publicClientID;
        }
        public override async Task                     GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
            {
            var claims = new List<Claim>();
            claims.Add(new Claim(ClaimTypes.Name, context.UserName));
            ClaimsIdentity oAuthClaimIdentity = new ClaimsIdentity(claims,         OAuthDefaults.AuthenticationType);
            ClaimsIdentity cookiesClaimIdentity = new ClaimsIdentity(claims,     CookieAuthenticationDefaults.AuthenticationType);
            AuthenticationProperties properties = CreateProperties(context.UserName);
            AuthenticationTicket ticket = new AuthenticationTicket(oAuthClaimIdentity, properties);
            context.Validated(ticket);
                context.Request.Context.Authentication.SignIn(cookiesClaimIdentity);
        }
        public static AuthenticationProperties CreateProperties(String UserName)
        {
            IDictionary<string, string> data = new Dictionary<string, string>
            {
                {
                    "UserName", UserName
                }
            };
            return new AuthenticationProperties(data);
        }
        public override Task TokenEndpoint(OAuthTokenEndpointContext context)
        {
            foreach(KeyValuePair<string, string> property in context.Properties.Dictionary)
            {
                context.AdditionalResponseParameters.Add(property.Key, property.Value);

            }
            return Task.FromResult<object>(null);
        }
        public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            if(context.ClientId == null)
            {
                context.Validated();
            }
            return Task.FromResult<object>(null);
        }
        public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context)
        {
            if(context.ClientId == _PublicClientID)
            {
                Uri expectedRootUri = new Uri(context.Request.Uri, "/");
               if(expectedRootUri.AbsoluteUri == context.RedirectUri)
                {
                    context.Validated();
                }

            }
            return Task.FromResult<object>(null);
        }

    }
}

这是我的WebApiConfig:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
//using System.Web.Http.OData.Builder;
//using System.Web.Http.OData.Extensions;
using System.Data.Entity;
using ApiStad.Models;
using Microsoft.Owin.Security.OAuth;

namespace ApiStad
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
            config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<benutzer>("Benutzer");
            builder.EntitySet<fortschritt>("Fortschritt");
            config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
        }
    }
}

0 个答案:

没有答案