public async Task <OpenIdModel> GetOpenIdAsync(string accessToken, CancellationToken cancellationToken) { var openIdUrl = OpenIdEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken); var response = await GetStringResponseAsync(openIdUrl, cancellationToken); return(OpenIdModel.From(response)); }
private void SetErrorOpenIdModel(OpenIdModel model, string msg) { model.OpenId = string.Empty; model.SessionKey = string.Empty; model.UnionId = string.Empty; model.IsSuccess = false; model.ReturnMsg = msg; }
private OpenIdModel GetOpenIdMethod(string code) { var model = new OpenIdModel(); var updateBaseInfoModel = GetUpdateBaseInfo(); if (updateBaseInfoModel.IsSuccess) { model.IsSuccess = updateBaseInfoModel.IsSuccess; model.ReturnMsg = updateBaseInfoModel.ReturnMsg; var url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + updateBaseInfoModel.AppId + "&secret=" + updateBaseInfoModel.AppSecret + "&js_code=" + code + "&grant_type=authorization_code"; try { var result = RequestUtilityHelper.HttpGet(url, encoding: Encoding.UTF8, timeOut: 30000); var returnResult = System.Web.Helpers.Json.Decode <OpenIdModelResultModel>(result); if (string.IsNullOrEmpty(returnResult.errcode)) { model.OpenId = returnResult.openid; model.SessionKey = returnResult.session_key; model.UnionId = returnResult.unionid; model.IsSuccess = true; model.ReturnMsg = "调用成功"; } else { SetErrorOpenIdModel(model, "错误码:" + returnResult.errcode + ",错误信息:" + returnResult.errmsg); } } catch (Exception ex) { SetErrorOpenIdModel(model, ex.Message); } } else { SetErrorOpenIdModel(model, updateBaseInfoModel.ReturnMsg); } return(model); }
public static ClaimsIdentity BuildClaimsIdentity(string issuer, TokenModel token, OpenIdModel openId, UserModel user) { var claims = new List <Claim> { new Claim(System.Security.Claims.ClaimTypes.NameIdentifier, openId.OpenId, ClaimValueTypes.String, issuer), new Claim(ClaimTypes.Issuer, issuer, ClaimValueTypes.String, issuer), new Claim(ClaimTypes.ClientId, openId.ClientId, ClaimValueTypes.String, issuer), new Claim(ClaimTypes.OpenId, openId.OpenId, ClaimValueTypes.String, issuer), new Claim(ClaimTypes.AccessToken, token.AccessToken, ClaimValueTypes.String, issuer), new Claim(ClaimTypes.RefreshToken, token.RefreshToken, ClaimValueTypes.String, issuer), new Claim(ClaimTypes.ExpiresIn, token.ExpiresIn, ClaimValueTypes.String, issuer), new Claim(ClaimTypes.NickName, user.NickName, ClaimValueTypes.String, issuer), new Claim(ClaimTypes.Avatar, user.Avatar, ClaimValueTypes.String, issuer), }; return(new ClaimsIdentity( claims, issuer, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType)); }