示例#1
0
        /// <summary>
        /// 请求登陆,获得用户信息。<para />
        /// 无论登录成功与否,该方法都会直接进行 HTTP 响应登录结果,使用该方法后无需再进行 HTTP 响应。<para />
        /// </summary>
        /// <returns>登录成功后获得用户信息</returns>
        /// <exception cref="LoginServiceException">
        /// 如果登录失败,将会抛出异常。
        /// </exception>
        public UserInfo Login()
        {
            var code          = GetHeader(Constants.WX_HEADER_CODE);
            var encryptedData = GetHeader(Constants.WX_HEADER_ENCRYPTED_DATA);
            var iv            = GetHeader(Constants.WX_HEADER_IV);

            LoginResult loginResult = null;

            try
            {
                var api = new AuthorizationAPI();
                loginResult = api.Login(code, encryptedData, iv);
            }
            catch (Exception apiError)
            {
                var error = new LoginServiceException(Constants.ERR_LOGIN_FAILED, apiError.Message, apiError);
                Response.WriteJson(JsonForError(error));
                throw error;
            }

            var json = PrepareResponseJsonDictionary();

            json["session"] = new { id = loginResult.Id, skey = loginResult.Skey };
            Response.WriteJson(json);
            return(loginResult.UserInfo);
        }
示例#2
0
        /// <summary>
        /// 检查当前请求是否包含已登录的会话
        /// </summary>
        /// <returns>如果已登录,返回当前用户信息</returns>
        /// <exception cref="LoginServiceException">
        /// 如果检查登录失败,或者当前用户登录态不正确,将会抛出异常
        /// </exception>
        public UserInfo Check()
        {
            string id   = GetHeader(Constants.WX_HEADER_ID);
            string skey = GetHeader(Constants.WX_HEADER_SKEY);

            CheckLoginResult checkLoginResult = null;

            try
            {
                var api = new AuthorizationAPI();
                checkLoginResult = api.CheckLogin(id, skey);
            }
            catch (Exception apiError)
            {
                LoginServiceException error = null;
                if (apiError is AuthorizationAPIException)
                {
                    AuthorizationAPIException authError = (AuthorizationAPIException)apiError;
                    if (authError.Code == 60011 || authError.Code == 60012)
                    {
                        error = new LoginServiceException(Constants.ERR_INVALID_SESSION, authError.Message, authError);
                    }
                }
                if (error == null)
                {
                    error = new LoginServiceException(Constants.ERR_CHECK_LOGIN_FAILED, apiError.Message, apiError);
                }
                Response.WriteJson(this.JsonForError(error));
                throw error;
            }
            return(checkLoginResult.UserInfo);
        }
示例#3
0
        private string GetHeader(string headerName)
        {
            var headerValue = Request.Headers[headerName];

            if (String.IsNullOrEmpty(headerValue))
            {
                var error = new LoginServiceException(Constants.ERR_INVALID_REQUEST, $"请求头不包含 {headerName},请配合客户端 SDK 登陆后再进行请求");
                Response.WriteJson(JsonForError(error));
                throw error;
            }

            return(headerValue);
        }