【问题标题】:JWT - Configuring a Authorization Server and setting the issuer as itselfJWT - 配置授权服务器并将颁发者设置为自身
【发布时间】:2017-04-26 16:25:28
【问题描述】:

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

但是,我想将我的本地服务器(即运行项目的服务器)指定为 CustomJwtFormattingissuer。所以,在 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 错误:

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

【问题讨论】:

    标签: asp.net jwt oauth2


    【解决方案1】:

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

    check my request

    【讨论】:

      【解决方案2】:

      好吧,我已经尝试将 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`
      

      它成功了。

      【讨论】:

        猜你喜欢
        • 2020-03-14
        • 2018-08-30
        • 2021-11-27
        • 2019-12-15
        • 1970-01-01
        • 2015-08-26
        • 2016-08-12
        • 1970-01-01
        • 2017-08-09
        相关资源
        最近更新 更多