Tag: jwt

如何使用System.IdentityModel.Tokens.Jwt使用Google OAuth2兼容算法RSA SHA-256生成JWT?

我正在尝试创建一个JWT来使用System.IdentityModel.Tokens.Jwt在Google文档中描述的服务帐户进行授权。 我有以下代码: byte[] key = Convert.FromBase64String(“…”); var certificate = new X509Certificate2(key, “notasecret”); DateTime now = DateTime.UtcNow; TimeSpan span = now – UnixEpoch; Claim[] claims = { new Claim(“iss”, “email@developer.gserviceaccount.com”), new Claim(“scope”, “https://www.googleapis.com/auth/plus.me”), new Claim(“aud”, “https://accounts.google.com/o/oauth2/token”), new Claim(“iat”, span.TotalSeconds.ToString()), new Claim(“exp”, span.Add(TimeSpan.FromHours(1)).TotalSeconds.ToString()) }; JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler(); var descriptor = new SecurityTokenDescriptor { SigningCredentials = […]

JWT和Web API(JwtAuthForWebAPI?) – 寻找一个例子

我有一个Angular前面的Web API项目,我想使用JWT令牌来保护它。 我已经进行了用户/通过validation,所以我认为我只需要实现JWT部分。 我相信我已经确定了JwtAuthForWebAPI所以使用它的一个例子会很棒。 我假设任何未使用[Authorize]修饰的方法都会像往常一样运行,并且如果客户端传递的令牌不匹配,则[Authorize]修饰的任何方法都将为401。 我还不知道如何在初始身份validation时将令牌发送回客户端。 我正在尝试使用魔术字符串开始,所以我有这个代码: RegisterRoutes(GlobalConfiguration.Configuration.Routes); var builder = new SecurityTokenBuilder(); var jwtHandler = new JwtAuthenticationMessageHandler { AllowedAudience = “http://xxxx.com”, Issuer = “corp”, SigningToken = builder.CreateFromKey(Convert.ToBase64String(new byte[]{4,2,2,6})) }; GlobalConfiguration.Configuration.MessageHandlers.Add(jwtHandler); 但我不确定最初是如何回到客户端的。 我想我理解如何在客户端上处理这个问题,但是如果你还可以展示这种交互的Angular方面,还可以获得奖励积分。

如何在asp.net Core中使用JWT

我按照JWT身份validation为Asp.Net Web Api回答将JWT添加到我的asp.net核心项目但是.net core中不支持System.Web.Http.Filters ? 任何更好的保护Web API的解决方案???

忽略JWT中的签名

我有一个使用OpenId Connect的Web应用程序。 我创建了一个自签名证书,但它仍未由CA签名。 如何忽略签名validation? 这是我到目前为止: SecurityToken validatedToken = null; var tokenHandler = new JwtSecurityTokenHandler { Configuration = new SecurityTokenHandlerConfiguration { CertificateValidator = X509CertificateValidator.None }, }; TokenValidationParameters validationParams = new TokenValidationParameters() { ValidAudience = ConfigurationManager.AppSettings[“Audience”], ValidIssuer = ConfigurationManager.AppSettings[“Issuer”], AudienceValidator = AudienceValidator, ValidateAudience = true, ValidateIssuer = true }; return tokenHandler.ValidateToken(jwtToken, validationParams, out validatedToken); 它抛出以下exception: IDX10500:签名validation失败。 无法解析SecurityKeyIdentifier:’SecurityKeyIdentifier […]

解码Azure移动服务JWT令牌时出现JwtSecurityTokenexception

以下代码: using System.IdentityModel.Tokens; JwtSecurityToken jwtSecurityToken = new JwtSecurityToken(rawToken); 生成以下exception: Jwt10113: Unable to decode the ‘header’ The value “0” is not of type “System.String” and cannot be used in this generic collection. Parameter name: value 当rawToken的’header’部分是: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6MH0 哪个可以解码为: {“alg”:”HS256″,”typ”:”JWT”,”kid”:0} 错误令牌的来源是Azure Mobile Services 。 请注意,调用同一行代码时不会发生exception,而’header’部分是: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjAifQ 哪个可以解码为: {“alg”:”HS256″,”typ”:”JWT”,”kid”:”0″} 我如何克服这个问题,并正确validation这样的令牌?

将.net core 2的JWT Authentication实现转移到asp.net web api 2

我在.net core 2应用程序中实现了JWT身份validation ,它工作正常。 我想在asp.net web api 2应用程序中使用此实现和结构但我收到错误 我的结构: JwtTokenBuilder类: using System; using System.Collections.Generic; using Microsoft.IdentityModel.Tokens; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Linq; namespace solution.Authentication { public sealed class JwtTokenBuilder { private SecurityKey securityKey = null; private string subject = “”; private string issuer = “”; private string audience = “”; private Dictionary claims = new […]

用于Azure移动服务(REST)的实时连接身份validation令牌

我有一个简单的Windows窗体应用程序来尝试Live Connect和Azure移动服务。 不是Windows 8现代应用程序。 我正在尝试使用Live Connect的一些示例代码,并且能够使登录部分正常工作。 最后,我在登录后获得了access_token。 在同一Windows窗体应用程序的移动服务端,我正在尝试使用REST登录。 但是,我希望获得“401 Unauthorized”响应,因为“身份validation令牌的签名格式错误或由其他密钥签名” JWT好像有两个’。’ 在令牌中。 SWT似乎有’/’,’+’和’=’ 我需要进行转换吗? 有没有办法配置Live Connect,以便返回的身份validation令牌是JWT(或移动服务所期望的)? 我在Live Connect和Mobile Services上都检查了客户机密码,它们是正确的。

添加Azure Ad Oauth2 JWT令牌声明

我只是想知道是否有办法通过Azure门户添加或指定Azure Ad OAuth2 JWT令牌的自定义声明? 或者这只是可能的代码方面?

validationJWT签名时的SecurityTokenSignatureKeyNotFoundException

我正在尝试为我的组织实施OpenID Connect规范。 我在测试依赖方应用程序中使用Microsoft的OpenID Connect的OWIN实现来validation我的协议实现。 我已经公开了以下元数据文档: { “issuer”: “https://acs.contoso.com/”, “authorization_endpoint”: “http://localhost:53615/oauth2/auth”, “token_endpoint”: “http://localhost:53615/oauth2/token”, “userinfo_endpoint”: “http://localhost:53615/connect/userinfo”, “jwks_uri”: “http://localhost:53615/connect/keys”, “ui_locales_supported”: [ “en-GB” ] } 签名密钥作为此文档公开: { “keys”: [ { “n”: “xpXxl3M-YkZlzQJdArO1TfOGT2no-UL4dbZ7WuSCNIsSfyGDaqUXjMMHNyq9yD3vp-NCyk8kmn7d5XqHufnceXJM8q4xTrhN3lvywdBSbR-dwXsA-B-MJVgfiK0d_z-mxP9ew2Hj9-KkWbWCzsswlWp3gZ4mB4RGutB1IRSzXVIbvZ-MtKUb6XUDU4LDb_c1xCEXWZxhR-o1a1dLfObH2hHJ-w5y6odGlKtOFx4i4h0u7-Oj5R6k5b2YXEHM0IuYeN0u0sQvrTecokntGzPrvhnKy69I7Z_az5rC5kgloh25D9lTbe4vcRU7FXlYCFYDZsT0_IkGIXRi7brOS4f1ow”, “e”: “AQAB”, “kty”: “RSA”, “use”: “sig”, “alg”: “RS256”, “kid”: “F8A59280B3D13777CC7541B3218480984F421450” } ] } 使用JwtSecurityToken类使用JwtSecurityToken类及其关联的处理程序生成标识令牌。 此代码表示如何构造令牌并将其作为响应数据的参数返回给调用系统。 var credentials = new X509SigningCredentials(cert); // My certificate. var issuedTime = DateTime.UtcNow; […]

为Firebase生成JWT

我正在尝试在Windows上为C#.NET编写一个自定义JWT令牌生成器,以便在Google Firebase中使用。 我从StackOverflow上的几个不同的地方取消了源代码(我正在尝试重新发现它们,所以我可以相信原作者并提出一些额外的问题),所以这有点像一个混蛋。 在大多数情况下它似乎工作,但它似乎没有按预期生成有效(firebase)令牌。 这可能完全是由于我首先对它应该如何工作缺乏了解,但是如果我能从这里得到第二对世界级的工程师眼睛来指出我做错了什么并使事情有效,这会很好。 这是代码: // Token generator setup using Newtonsoft.Json; using System; using System.IO; using System.Web; using System.Web.Configuration; namespace myapp.Utils { public class GoogleJsonWebToken { public static string Encode(string uid) { var utc0 = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); var issueTime = DateTime.Now; var iat = (int)issueTime.Subtract(utc0).TotalSeconds; var exp […]