/// <summary> /// 微信网页授权,如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。 /// </summary> /// <param name="redirect_uri">授权后重定向的回调链接地址</param> /// <param name="state">重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节</param> /// <param name="scope">应用授权作用域</param> /// <returns></returns> public static string OAuthToURL(string redirect_uri, string state = "", scope scope = scope.snsapi_base) { if (string.IsNullOrWhiteSpace(redirect_uri)) { throw new ArgumentException("参数值无效", nameof(redirect_uri)); } return(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state={3}#wechat_redirect" , Privacy.AppId , HttpUtility.UrlEncode(redirect_uri) , scope.ToString() , state )); }
/// <summary> /// 引导用户进入授权页面同意授权,获取code /// 如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。 /// 若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE /// (注意:重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值) /// </summary> /// <param name="redirect_uri">目标页面URL(不包含页面参数)</param> /// <param name="scope">应用授权作用域</param> /// <param name="state">重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值</param> /// <returns></returns> public static string OAuthToURL(string redirect_uri, scope scope, string state) { if (string.IsNullOrEmpty(redirect_uri)) { throw new Exception("redirect_uri 为必填参数!"); } if (scope == null) { scope = scope.snsapi_login; } if (string.IsNullOrEmpty(state)) { state = string.Empty; } return(string.Format("https://open.weixin.qq.com/connect/qrconnect?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state={3}#wechat_redirect" , Privacy.AppId , HttpUtility.UrlEncode(redirect_uri) , scope.ToString() , state )); }
/// <summary> /// 第一步:用户同意授权,获取code /// </summary> /// <param name="cope"></param> /// <param name="state"></param> /// <returns></returns> public string GetAuthorizeURL(scope cope, string state) { string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Appid + "&redirect_uri=" + CallbackUrl + "&response_type=" + code + "&scope=" + cope.ToString() + "&state=" + state + "#wechat_redirect"; return(url); }