/// <summary> /// 构造网页授权跳转的Url /// </summary> /// <param name="appID"></param> /// <param name="redirect_uri">重定向后跳转的地址</param> /// <param name="type">授权类型</param> /// <param name="state">附带数据</param> /// <returns></returns> public string BuildOAuthUrl(string appID, string redirect_uri, SnsapiType type = SnsapiType.Base, string state = "") { if (!string.IsNullOrWhiteSpace(state)) { if (state.Length > 128) { throw new ArgumentOutOfRangeException(nameof(state), "state参数不能超过128个字符"); } } var url = $"https://open.weixin.qq.com/connect/oauth2/authorize?appid={appID}&redirect_uri={Uri.EscapeUriString(redirect_uri)}&response_type=code&scope={(type == SnsapiType.Base ? "snsapi_base" : "snsapi_userinfo")}&state={Uri.EscapeUriString(state.ToStringEx())}#wechat_redirect"; return(url); }
public WechatMPAuthorizeAttribute(string scheme = "wechat", SnsapiType oauthType = SnsapiType.Base) : base() { this.AuthenticationSchemes = scheme; OAuthType = oauthType; }
/// <summary> /// 授权登录 /// </summary> /// <param name="appid"></param> /// <param name="reurl"></param> /// <param name="state"></param> /// <returns></returns> public static Uri GetWxWebLogin(SnsapiType snsapi, string appid, string reurl, string state) { return(new Uri($"https://open.weixin.qq.com/connect/oauth2/authorize?appid={appid}&redirect_uri={HttpUtility.UrlEncode(reurl)}&response_type=code&scope={snsapi.ToString()}&state={state}#wechat_redirect")); }