예제 #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);
        }