public static string PayNotifyToToken(this PayNotify arg, string secret) { List <Claim> list = new List <Claim>(); Claim Amount = new Claim(PayNotifyClaimTypes.Amount, arg.Amount.ToString()); list.Add(Amount); Claim OrderIds = new Claim(PayNotifyClaimTypes.OrderIds, arg.OrderIds); list.Add(OrderIds); Claim TradNo = new Claim(PayNotifyClaimTypes.TradNo, arg.TradNo); list.Add(TradNo); Claim ChannelName = new Claim(PayNotifyClaimTypes.ChannelName, arg.ChannelName); list.Add(ChannelName); Claim ResponseContentWhenFinished = new Claim(PayNotifyClaimTypes.ResponseContentWhenFinished, arg.ResponseContentWhenFinished); list.Add(ResponseContentWhenFinished); Claim TradeTime = new Claim(PayNotifyClaimTypes.TradeTime, arg.TradeTime.ToString()); list.Add(TradeTime); DefaultJwtPrincipalBuilder jwtBuilder = new DefaultJwtPrincipalBuilder(secret); return(jwtBuilder.BuildToken(list)); }
public override IActionResult DoSignIn(string AuthenticationScheme, ClaimsPrincipal pricipal) { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(this.WeiXinMpConfig.JwtSecret); //return Content(builder.BuildToken(pricipal.Claims)); return(Redirect($"{this.WeiXinMpConfig.CallBackUrl}?token={builder.BuildToken(pricipal.Claims)}")); }
public static PayNotify PayNotifyFromToken(this string token, string secret) { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(secret); var claimsPrincipal = builder.GetPrincipal("paynotifytoken", token); if (claimsPrincipal.IsNull()) { return(default);
public static PaymentAppToken ToPaymentArgToken(this PaymentArg arg, string secret) { List <Claim> list = new List <Claim>(); Claim orderid = new Claim(PayargClaimTypes.OrderId, arg.BizOrderId); list.Add(orderid); Claim Amount = new Claim(PayargClaimTypes.Amount, arg.Amount.ToString()); list.Add(Amount); Claim ApiVersion = new Claim(PayargClaimTypes.ApiVersion, arg.ApiVersion); list.Add(ApiVersion); Claim AppName = new Claim(PayargClaimTypes.AppName, arg.AppName); list.Add(AppName); Claim AttachData = new Claim(PayargClaimTypes.AttachData, arg.AttachData); list.Add(AttachData); Claim BackUrl = new Claim(PayargClaimTypes.BackUrl, arg.BackUrl); list.Add(BackUrl); Claim ChannelName = new Claim(PayargClaimTypes.ChannelName, arg.ChannelName); list.Add(ChannelName); Claim MerchantId = new Claim(PayargClaimTypes.MerchantId, arg.MerchantId.ToString()); list.Add(MerchantId); Claim OpenId = new Claim(PayargClaimTypes.OpenId, arg.OpenId); list.Add(OpenId); Claim AppId = new Claim(PayargClaimTypes.AppId, arg.AppId); list.Add(AppId); Claim OrderInfo = new Claim(PayargClaimTypes.OrderInfo, arg.OrderInfo); list.Add(OrderInfo); Claim ReturnUrl = new Claim(PayargClaimTypes.ReturnUrl, arg.ReturnUrl); list.Add(ReturnUrl); DefaultJwtPrincipalBuilder jwtBuilder = new DefaultJwtPrincipalBuilder(secret); var token = jwtBuilder.BuildToken(list); PaymentAppToken pac = new PaymentAppToken() { Token = token, AppName = arg.AppName }; //var bs = arg.ToArray(); //pac.Code = System.Web.HttpUtility.UrlEncode(bs); //var symmetricKey = Convert.FromBase64String(secret); //HMACSHA256 h256 = new HMACSHA256(symmetricKey); //pac.Signature = System.Web.HttpUtility.UrlEncode(Convert.ToBase64String(h256.ComputeHash(bs))); return(pac); }
string buildWXAToken(AppConfig config, string OpenId, string UnionId, string NickName, string Avatar) { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(config.AuthSecret); List <Claim> list = new List <Claim>(); list.Add(new Claim(CissyWXATokenClaims.OpenId, OpenId)); list.Add(new Claim(CissyWXATokenClaims.UnionId, UnionId)); list.Add(new Claim(CissyWXATokenClaims.NickName, NickName)); list.Add(new Claim(CissyWXATokenClaims.Avatar, Avatar)); return(builder.BuildToken(list)); }
//public static PaymentArgResult FromUrlEncryptCode(this PaymentArgCode code, out PaymentArg arg, Func<PaymentArg, string> GetSecret) //{ // var bs = System.Web.HttpUtility.UrlDecodeToBytes(code.Code); // arg = SeriaHelper.DeserializeFrom<PaymentArg>(bs); // if (arg.IsNull()) // return new PaymentArgResult(PaymentArgResultStatus.Faild); // var plugin = Actor.Public.GetPaymentPlugin(arg.ChannelName); // if (plugin.IsNull()) // { // arg = default(PaymentArg); // return new PaymentArgResult(PaymentArgResultStatus.Faild); // } // if (plugin.NeedRedirect) // { // return new PaymentArgResult(PaymentArgResultStatus.Redirect) { Tie = plugin.Tie, UrlType = plugin.RequestUrlType }; // } // var secret = GetSecret(arg); // if (secret.IsNullOrEmpty()) // { // arg = default(PaymentArg); // return new PaymentArgResult(PaymentArgResultStatus.Faild) { Tie = plugin.Tie, UrlType = plugin.RequestUrlType }; // } // var symmetricKey = Convert.FromBase64String(secret); // HMACSHA256 h256 = new HMACSHA256(symmetricKey); // var b64 = Convert.ToBase64String(h256.ComputeHash(bs)); // //var sign = System.Web.HttpUtility.UrlEncode(b64); // if (code.Signature != b64) // return new PaymentArgResult(PaymentArgResultStatus.Faild) { Tie = plugin.Tie, UrlType = plugin.RequestUrlType }; // return new PaymentArgResult(PaymentArgResultStatus.Success) { Tie = plugin.Tie, UrlType = plugin.RequestUrlType }; //} public static PaymentArgResult FromPayToken(this PaymentAppToken apptoken, out PaymentArg arg, Func <string, string> GetSecret) { var secret = GetSecret(apptoken.AppName); if (secret.IsNullOrEmpty()) { arg = default(PaymentArg); return(new PaymentArgResult(PaymentArgResultStatus.Faild)); } DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(secret); var claimsPrincipal = builder.GetPrincipal("paytoken", apptoken.Token); if (claimsPrincipal.IsNull()) { arg = default(PaymentArg); return(new PaymentArgResult(PaymentArgResultStatus.Faild)); } arg = new PaymentArg(); arg.Amount = int.Parse(claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.Amount).Value); arg.ApiVersion = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.ApiVersion).Value; arg.AppName = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.AppName).Value; arg.AttachData = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.AttachData).Value; arg.BackUrl = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.BackUrl).Value; arg.BizOrderId = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.OrderId).Value; arg.ChannelName = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.ChannelName).Value; arg.MerchantId = long.Parse(claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.MerchantId).Value); arg.OpenId = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.OpenId).Value; arg.AppId = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.AppId).Value; arg.OrderInfo = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.OrderInfo).Value; arg.ReturnUrl = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == PayargClaimTypes.ReturnUrl).Value; //if (arg.IsNull()) // return new PaymentArgResult(PaymentArgResultStatus.Faild); var plugin = Actor.Public.GetPaymentPlugin(arg.ChannelName); if (plugin.IsNull()) { arg = default(PaymentArg); return(new PaymentArgResult(PaymentArgResultStatus.Faild)); } if (plugin.NeedRedirect) { return(new PaymentArgResult(PaymentArgResultStatus.Redirect) { Tie = plugin.Tie, UrlType = plugin.RequestUrlType }); } return(new PaymentArgResult(PaymentArgResultStatus.Success) { Tie = plugin.Tie, UrlType = plugin.RequestUrlType }); }
public async Task InvokeAsync(HttpContext context) { string token = string.Empty; if (Option.AuthenticationType == AuthenticationTypes.Cookie) { token = context.Request.Cookies[AuthenticationHelper.BuildCookieName(Option.Scheme)]; } else if (Option.AuthenticationType == AuthenticationTypes.Token) { string tokenAuthentication = context.Request.Headers["Authorization"]; if (tokenAuthentication != null && tokenAuthentication.Contains(Option.Scheme)) { token = tokenAuthentication.Trim().Split(" ")[1]; } } if (token.IsNotNullAndEmpty()) { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(this.AppConfig.AuthSecret); var claimsPrincipal = builder.GetPrincipal(Option.Scheme, token); if (claimsPrincipal.IsNotNull()) { context.User = claimsPrincipal; AuthorityContext ac = new AuthorityContext(); ac.User = claimsPrincipal; var cl = claimsPrincipal.Claims.FirstOrDefault(m => m.Type == CissyClaimTypes.Permits); if (cl.IsNotNull())//开始构建权限上下文 { //step1,收集当前身份具有的权限集 string[] vs = cl.Value.Split(AuthoritySeparators.Top, StringSplitOptions.RemoveEmptyEntries); foreach (string v in vs) { string[] ms = v.Split(AuthoritySeparators.FirstLevel); if (!ac.AuthorityScopes.TryGetValue(ms[0], out Dictionary <int, int> dic)) { dic = new Dictionary <int, int>(); ac.AuthorityScopes[ms[0]] = dic; } dic[int.Parse(ms[1])] = int.Parse(ms[2]); } //step2,搜索权限请求范围 var scope = _getAuthorityScope(context); scope = scope.IsNullOrEmpty() ? AuthoritySeparators.DefaultScope : scope.Trim(); ac.AskScope = scope; } context.Items[AuthorityContext.AuthorityContextKey] = ac; } } await _next.Invoke(context); }
public IActionResult SignIn(string AuthenticationScheme, string token) { try { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(WeiXinMpConfig.JwtSecret); var claimsPrincipal = builder.GetPrincipal(AuthenticationScheme, token); PreSignInAspect(claimsPrincipal.GetWeiXinPassport(), claimsPrincipal); return(DoSignIn(AuthenticationScheme, claimsPrincipal)); } catch (Exception ep) { return(Redirect("~/")); //return Content(AuthenticationScheme + "////" + ep.Message + "///" + ep.StackTrace); } }
public IActionResult GrantIn(string AuthenticationScheme, string token, string group, string grantcode) { try { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(appConfig.AuthSecret); var claimsPrincipal = builder.GetPrincipal(AuthenticationScheme, token); return(DoGrantIn(group, grantcode, claimsPrincipal.GetWeiXinPassport(), claimsPrincipal)); } catch (Exception ep) { return(Content($"{AuthenticationScheme}--{group}--{grantcode}--{token}")); //return Redirect("~/"); //return Content(AuthenticationScheme + "////" + ep.Message + "///" + ep.StackTrace); } }
public static BalanceAppToken ToBalanceAppToken(this BalanceArg arg, string secret) { List <Claim> list = new List <Claim>(); Claim orderid = new Claim(BalanceArg.BalanceClaimType, arg.BillJson); list.Add(orderid); DefaultJwtPrincipalBuilder jwtBuilder = new DefaultJwtPrincipalBuilder(secret); var token = jwtBuilder.BuildToken(list); BalanceAppToken pac = new BalanceAppToken() { Token = token, AppName = arg.AppName }; return(pac); }
public override IActionResult DoSignIn(string AuthenticationScheme, ClaimsPrincipal pricipal) { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(this.WeiXinMpConfig.JwtSecret); //return Content(builder.BuildToken(pricipal.Claims)); //var json = new JwtBuilder() // .WithSecret(_WeiXinMpConfig.JwtSecret) // .MustVerifySignature() //.Decode(builder.BuildToken(pricipal.Claims)); //return Content(json); //var t = from c in pricipal.Claims select new { c.Type, c.Value }; //var s = Newtonsoft.Json.JsonConvert.SerializeObject(t); //return Content(s); var url = $"{this.WeiXinMpConfig.CallBackUrl}?token={builder.BuildToken(pricipal.Claims)}"; //return Content(url); return(Redirect(url)); }
public async Task <IActionResult> GetAppAuthToken(string openId, string unionId, string nickName, string avatarUrl) { var wxaToken = buildWXAToken(this.appConfig, openId, unionId, nickName, avatarUrl); var url = Actor.Public.GetWxAppAuthUrl(this.appConfig, wxaToken); string token = string.Empty; if (await Actor.Public.GetAsync(url, m => { token = m; })) { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(this.appConfig.AuthSecret); var claimsPrincipal = builder.GetPrincipal(this.Option.Scheme, token); PreSignInAspect(claimsPrincipal.GetWeiXinPassport(), claimsPrincipal); DefaultJwtPrincipalBuilder b2 = new DefaultJwtPrincipalBuilder(this.appConfig.AuthSecret); return(Content(b2.BuildToken(claimsPrincipal.Claims))); } return(StatusCode(500)); }
public IActionResult SignIn(string AuthenticationScheme, string token) { try { //var json = new JwtBuilder() // .WithSecret(_WeiXinMpConfig.JwtSecret) // .MustVerifySignature() //.Decode(token); //return Content(json); DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(WeiXinMpConfig.JwtSecret); var claimsPrincipal = builder.GetPrincipal(AuthenticationScheme, token); PreSignInAspect(claimsPrincipal.GetWeiXinPassport(), claimsPrincipal); return(DoSignIn(AuthenticationScheme, claimsPrincipal)); } catch (Exception ep) { return(Redirect("~/")); //return Content(token + "////" + ep.Message + "///" + ep.StackTrace); } //return Content("final"); }
public IActionResult DoSignIn(string AuthenticationScheme, ClaimsPrincipal pricipal) { if (this.Option.AuthenticationType == AuthenticationTypes.Cookie) { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(this.appConfig.AuthSecret); //HttpContext.Response.Cookies.Append(AuthenticationHelper.BuildCookieName(this.Option.Scheme), builder.BuildToken(pricipal.Claims), new Microsoft.AspNetCore.Http.CookieOptions() //{ // Expires = DateTime.Now.AddDays(1), // //HttpOnly = true //}); HttpContext.Response.Cookies.Append(AuthenticationHelper.BuildCookieName(this.Option.Scheme), builder.BuildToken(pricipal.Claims)); //return Content(builder.BuildToken(pricipal.Claims)); return(Redirect(this.appConfig.AuthBackUrl)); } else if (this.Option.AuthenticationType == AuthenticationTypes.Token) { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(this.appConfig.AuthSecret); //return Content(builder.BuildToken(pricipal.Claims)); return(Redirect($"{this.appConfig.AuthBackUrl}?token={builder.BuildToken(pricipal.Claims)}")); } return(StatusCode(500)); }
public async Task <IActionResult> GetAuthToken(string code, string encryptedData, string iv) { if (appConfig.IsNull()) { return(StatusCode(500)); } var result = await WeiXinMpApi.Code2SessionAsync(code); if (result.IsNull()) { return(StatusCode(500)); } var session_key = result.session_key; var json = WXEncryptHelper.DecodeEncryptedData(session_key, encryptedData, iv); var userInfo = json.JsonToModel <DecodedUserInfo>(); if (userInfo.unionId.IsNullOrEmpty()) { return(StatusCode(406)); } var wxaToken = buildWXAToken(this.appConfig, userInfo.openId, userInfo.unionId, userInfo.nickName, userInfo.avatarUrl); var url = Actor.Public.GetWxAppAuthUrl(this.appConfig, wxaToken); string token = string.Empty; if (await Actor.Public.GetAsync(url, m => { token = m; })) { DefaultJwtPrincipalBuilder builder = new DefaultJwtPrincipalBuilder(this.appConfig.AuthSecret); var claimsPrincipal = builder.GetPrincipal(this.Option.Scheme, token); PreSignInAspect(claimsPrincipal.GetWeiXinPassport(), claimsPrincipal); DefaultJwtPrincipalBuilder b2 = new DefaultJwtPrincipalBuilder(this.appConfig.AuthSecret); return(Content(b2.BuildToken(claimsPrincipal.Claims))); } return(StatusCode(500)); }