JWT - Configuring a Authorization Server and setting the issuer as itself

拥有回忆 提交于 2019-12-10 10:16:54

问题


I am trying to set up an Authorization Server following this guide: http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/

However, I want to assign my local server (i.e. the server that the project runs on) as the issuer for CustomJwtFormatting. So, in Startup.cs I use:

    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);
    }

And for the CustomJwtFormat class, the code is like that:

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);

However, I get a 404 error when I send a POST request to http://127.0.0.1/oauth2/token :

What is the best way to achieve that issuer-on-the-local-server thing correctly in ASP.NET?


回答1:


You need to provide the clientId in the Authorization header and set its type to Basic, consider that i have also encoded the clientId

check my request




回答2:


Well, I have tried assigning null to the issuer like

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`

and it worked.



来源:https://stackoverflow.com/questions/41090102/jwt-configuring-a-authorization-server-and-setting-the-issuer-as-itself

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!