IdentityServer3 ResourceOwner Angular 请求 returns 400 错误请求

IdentityServer3 ResourceOwner Angular request returns 400 Bad request

我有一个 identityServer3 示例应用程序。

public static IEnumerable<Client> GetClients()
        {
            return new[]
                   {
                       new Client
                       {
                           Enabled = true,
                           ClientId = "manager",
                           ClientName = "ManagerIdentity",
                           Flow = Flows.ResourceOwner,
                           ClientSecrets = new List<Secret>
                           {
                               new Secret("secret".Sha256())
                           },
                           AllowedScopes = new List<string>
                           {
                               Constants.StandardScopes.OpenId
                           },
                           AllowAccessTokensViaBrowser = true,
                           AllowedCorsOrigins = new  List<string>{ 
                              "http://localhost:24678/" // angular application uri
                           }
                       }  
            }

我正在使用此信息并通过邮递员和令牌请求 returns。

但我通过 angularjs javascript 应用程序发送相同的请求 returns 400 错误请求。

angular.module("app").controller("ROPCController", [
    "$scope","$http",
    function($scope,$http) {

        $scope.login = function() {


            var options = {
                "url": "http://localhost:4751/connect/token",
                "method": "POST",
                "data": {
                    "username": "muser",
                    "password": "password",
                    "grant_type": "password",
                    "client_id": "manager",
                    "client_secret": "secret",
                    "scope":"openid"
                },
                "headers": {
                     "Content-Type": "application/x-www-form-urlencoded"
                }
            };

            $http(options).then(
                function(response) {
                    console.log(response.data);
                },
                function(error) {
                    console.log(error);
                }
            );
        }
    }
]);

{"error":"invalid_client"}

您的数据采用 json 格式,因此您应将内容类型更改为 application/json 或将数据格式更改为 username=muser&password=password&...

希望对您有所帮助。