JWT - 配置授权服务器并将颁发者设置为自身

时间:2016-12-11 19:19:30

标签: asp.net jwt oauth2

我正在尝试按照本指南设置授权服务器: http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/

但是,我想将本地服务器(即项目运行的服务器)指定为 CustomJwtFormatting 颁发者。所以,在Startup.cs中我使用:

    public void ConfigureOAuth(IAppBuilder app)
    {
        var issuer = HttpContext.Current.Request.Url.Scheme + System.Uri.SchemeDelimiter + HttpContext.Current.Request.Url.Host
            + (HttpContext.Current.Request.Url.IsDefaultPort ? "" : ":" + HttpContext.Current.Request.Url.Port); // get the host name with the port

        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            //For Dev enviroment only (on production should be AllowInsecureHttp = false)
            //TODO: Make it false before going live
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/oauth2/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
            Provider = new CustomOAuthProvider(),
            AccessTokenFormat = new CustomJwtFormat(issuer)
        };

        // OAuth 2.0 Bearer Access Token Generation
        app.UseOAuthAuthorizationServer(OAuthServerOptions);
    }

对于CustomJwtFormat类,代码就是这样:

var issuer = HttpContext.Current.Request.Url.Scheme + System.Uri.SchemeDelimiter + HttpContext.Current.Request.Url.Host + (HttpContext.Current.Request.Url.IsDefaultPort ? "" : ":" + HttpContext.Current.Request.Url.Port);  // get the host name with the port
var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);

但是,当我向http://127.0.0.1/oauth2/token发送POST请求时,我收到404错误:

Postmnan Results

在ASP.NET中正确实现发布者在本地服务器上的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

您需要在Authorization标头中提供clientId并将其类型设置为Basic,考虑我还编码了clientId

check my request

enter image description here

答案 1 :(得分:0)

好吧,我已经尝试将null分配给发行者,如

var issuer = null;
//var issuer = HttpContext.Current.Request.Url.Scheme + System.Uri.SchemeDelimiter + HttpContext.Current.Request.Url.Host + (HttpContext.Current.Request.Url.IsDefaultPort ? "" : ":" + HttpContext.Current.Request.Url.Port); // get the host name with the port`

并且有效。