public string GetUrlPublic(string token, string urlRedirect) { string urlRoot = SPContext.Current.Site.Url; if (!string.IsNullOrEmpty(token)) { try { byte[] tokenByte = Convert.FromBase64String(token); string tokenDecode = Encoding.UTF8.GetString(tokenByte); string validate = Common.ValidateJWT(tokenDecode); PayloadJWT payloadJWT = JsonConvert.DeserializeObject <PayloadJWT>(validate); string tokenDecryp = string.IsNullOrEmpty(payloadJWT.hashpwd) ? "" : Common.doDecryptAES(payloadJWT.hashpwd); string user = payloadJWT.context.user.userName; string pass = ConfigurationManager.AppSettings["sqlAuth"] == "yes" ? $"{user}@123" : tokenDecryp; bool login = SPClaimsUtility.AuthenticateFormsUser(new Uri(urlRoot), user, pass); if (login) { WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Redirect; WebOperationContext.Current.OutgoingResponse.Location = urlRedirect; var response = HttpContext.Current.Response; response.Cookies.Remove("token"); HttpCookie cookies = new HttpCookie("token") { Value = tokenDecode, //EncryptString(usrName + ":" + pwd); Expires = DateTime.Now.AddDays(1d) }; response.Cookies.Add(cookies); } else { WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden; } } catch (Exception ex) { throw ex; } } return(""); }
public static string CreateJWT(PayloadJWT payload) { try { if (string.IsNullOrEmpty(secretJWT)) { secretJWT = ConfigurationManager.AppSettings["secretJWT"] != null ? ConfigurationManager.AppSettings["secretJWT"] : initVector; } IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); // symmetric IJsonSerializer serializer = new JsonNetSerializer(); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder); var token = encoder.Encode(payload, secretJWT); return(token); } catch (Exception) { return("Error"); } }