public ActionResult AuthTokenGenerate()
 {
     string userId = Session[USERID_KEY] as string;
     if (userId == null)
         return HttpNotFound("not logged in");
     var token = new AuthToken(userId);
     var key = ConfigurationManager.AppSettings["Auth.SecretKey"];
     var tokenStr = JsonWebToken.Encode(token, key, JwtHashAlgorithm.HS512);
     return Content(tokenStr);
 }
 public ActionResult AuthTokenGet(String state)
 {
     string userId = Session[USERID_KEY] as string;
     if (userId == null)
         return Redirect("/Login?exAuthState="+state);
     var token = new AuthToken(userId);
     var key = ConfigurationManager.AppSettings["Auth.SecretKey"];
     var tokenStr = JsonWebToken.Encode(token, key, JwtHashAlgorithm.HS512);
     var redirect = ConfigurationManager.AppSettings["Auth.Redirect"];
     return Redirect(redirect+"?token="+tokenStr+"&state="+state);
 }
Example #3
0
 public static string Encode(AuthToken payload, string key, JwtHashAlgorithm algorithm)
 {
     return Encode(payload, Encoding.UTF8.GetBytes(key), algorithm);
 }
Example #4
0
        public static string Encode(AuthToken payload, byte[] keyBytes, JwtHashAlgorithm algorithm)
        {
            var segments = new List<string>();
            var header = new { alg = algorithm.ToString(), typ = "JWT" };

            byte[] headerBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(header, Formatting.None));
            byte[] payloadBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload, Formatting.None));
            //byte[] payloadBytes = Encoding.UTF8.GetBytes(@"{"iss":"*****@*****.**","scope":"https://www.googleapis.com/auth/prediction","aud":"https://accounts.google.com/o/oauth2/token","exp":1328554385,"iat":1328550785}");

            segments.Add(Base64UrlEncode(headerBytes));
            segments.Add(Base64UrlEncode(payloadBytes));

            var stringToSign = string.Join(".", segments.ToArray());

            var bytesToSign = Encoding.UTF8.GetBytes(stringToSign);

            byte[] signature = HashAlgorithms[algorithm](keyBytes, bytesToSign);
            segments.Add(Base64UrlEncode(signature));

            return string.Join(".", segments.ToArray());
        }
 public static string Encode(AuthToken payload, string key, JwtHashAlgorithm algorithm)
 {
     return(Encode(payload, Encoding.UTF8.GetBytes(key), algorithm));
 }