验证持有者JWT

时间:2015-07-09 08:37:46

标签: c# authentication oauth-2.0 asp.net-web-api2 jwt

我对Web API很陌生,我仍然不知道如何请求n JWT。 在我的 Startup.cs 中,我配置了OAuth和我的自定义JWT:

CustomJwtFormat.cs

public class CustomJwtFormat : ISecureDataFormat<AuthenticationTicket>
{
    private readonly string issuer = string.Empty;
    private readonly int timeoutMinutes = 60;

    public CustomJwtFormat(string issuer)
    {
        this.issuer = issuer;
    }

    public string Protect(AuthenticationTicket data)
    {
        if (data == null)
        {
            throw new ArgumentNullException("data");
        }

        string audience = "all";
        var secret = Convert.FromBase64String("mySecret");
        var now = DateTime.UtcNow;
        var expires = now.AddMinutes(timeoutMinutes);
        string signatureAlgorithm = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256";
        string digestAlgorithm = "http://www.w3.org/2001/04/xmlenc#sha256";

        var signingKey = new SigningCredentials(
                new InMemorySymmetricSecurityKey(secret), signatureAlgorithm, digestAlgorithm);

        var token = new JwtSecurityToken(issuer, audience, data.Identity.Claims,
                                         now, expires, signingKey);

        var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
        return tokenString;
    }

    public AuthenticationTicket Unprotect(string protectedText)
    {
        throw new NotImplementedException();
    }
}
编辑:我使用Postman管理检索令牌。 Postman Token Request

现在我的下一个问题是:只要我发送一个有效的jwt,一切正常。但是当我发送时,例如持票人12345&#39;我收到错误消息

  

IDX10708:&#39; System.IdentityModel.Tokens.JwtSecurityTokenHandler&#39;无法读取此字符串:&#39; 12345&#39;。

...在Visual Studio中。我需要在哪里放入令牌验证?创建一个新类并将TokenSecurityHandler放在任何地方?

0 个答案:

没有答案