public LoginInfo Login(string UserName, string Password, string Client_identifier = "", string Language = "zh-cn") { LoginInfo LI = new LoginInfo(); ClientToken = Client_identifier; try { HttpWebRequest auth = (HttpWebRequest)WebRequest.Create(RouteAuthenticate); auth.Method = "POST"; AuthenticationRequest ag = new AuthenticationRequest(UserName, Password); DataContractJsonSerializer agJsonSerialiaer = new DataContractJsonSerializer(typeof(AuthenticationRequest)); MemoryStream agJsonStream = new MemoryStream(); agJsonSerialiaer.WriteObject(agJsonStream, ag); agJsonStream.Position = 0; string logindata = (new StreamReader(agJsonStream)).ReadToEnd(); byte[] postdata = Encoding.UTF8.GetBytes(logindata); auth.ContentLength = postdata.LongLength; Stream poststream = auth.GetRequestStream(); poststream.Write(postdata, 0, postdata.Length); poststream.Close(); HttpWebResponse authans = (HttpWebResponse)auth.GetResponse(); DataContractJsonSerializer ResponseJsonSerializer = new DataContractJsonSerializer(typeof(AuthenticationResponse)); StreamReader ResponseStream = new StreamReader(authans.GetResponseStream()); string ResponseJson = ResponseStream.ReadToEnd(); MemoryStream ResponseJsonStream = new MemoryStream(Encoding.UTF8.GetBytes(ResponseJson)); ResponseJsonStream.Position = 0; AuthenticationResponse Response = ResponseJsonSerializer.ReadObject(ResponseJsonStream) as AuthenticationResponse; if (Response.getClientToken() != NewLogin.ClientToken) { LI.Suc = false; LI.Errinfo = "客户端标识和服务器返回不符,这是个不常见的错误,就算是正版启动器这里也没做任何处理,只是报了这么个错。"; return LI; } LI.Suc = true; LI.UN = Response.getSelectedProfile().getName(); LI.Client_identifier = NewLogin.ClientToken; DataContractSerializer OtherInfoSerializer = new DataContractSerializer(typeof(SortedList)); SortedList OtherInfoList = new SortedList(); OtherInfoList.Add("${auth_uuid}",Response.getSelectedProfile().getId()); OtherInfoList.Add("${auth_access_token}", Response.getAccessToken()); MemoryStream OtherInfoStream = new MemoryStream(); OtherInfoSerializer.WriteObject(OtherInfoStream, OtherInfoList); OtherInfoStream.Position = 0; LI.OtherInfo = (new StreamReader(OtherInfoStream)).ReadToEnd(); return LI; } catch (TimeoutException ex) { LI.Suc = false; LI.Errinfo = ex.Message; return LI; } }
public LoginInfo Login(string userName, string password, string clientIdentifier = "", string language = "zh-cn") { var li = new LoginInfo(); _clientToken = clientIdentifier; try { var auth = (HttpWebRequest)WebRequest.Create(RouteAuthenticate); auth.Method = "POST"; var ag = new AuthenticationRequest(userName, password); var agJsonSerialiaer = new DataContractJsonSerializer(typeof(AuthenticationRequest)); var agJsonStream = new MemoryStream(); agJsonSerialiaer.WriteObject(agJsonStream, ag); agJsonStream.Position = 0; string logindata = (new StreamReader(agJsonStream)).ReadToEnd(); byte[] postdata = Encoding.UTF8.GetBytes(logindata); auth.ContentLength = postdata.LongLength; Stream poststream = auth.GetRequestStream(); poststream.Write(postdata, 0, postdata.Length); poststream.Close(); var authans = (HttpWebResponse)auth.GetResponse(); var responseJsonSerializer = new DataContractJsonSerializer(typeof(AuthenticationResponse)); var res = authans.GetResponseStream(); if (res == null) { li.Suc = false; li.Errinfo = "服务器无响应"; return(li); } var responseStream = new StreamReader(res); string responseJson = responseStream.ReadToEnd(); var responseJsonStream = new MemoryStream(Encoding.UTF8.GetBytes(responseJson)) { Position = 0 }; var response = responseJsonSerializer.ReadObject(responseJsonStream) as AuthenticationResponse; if (response == null) { li.Suc = false; li.Errinfo = "服务器无响应"; return(li); } if (response.getClientToken() != NewLogin._clientToken) { li.Suc = false; li.Errinfo = "客户端标识和服务器返回不符,这是个不常见的错误,就算是正版启动器这里也没做任何处理,只是报了这么个错。"; return(li); } li.Suc = true; li.UN = response.getSelectedProfile().getName(); li.Client_identifier = NewLogin._clientToken; li.SID = response.getAccessToken(); var otherInfoSerializer = new DataContractJsonSerializer(typeof(SortedList)); var otherInfoList = new SortedList { { "${auth_uuid}", response.getSelectedProfile().getId() }, { "${auth_access_token}", response.getAccessToken() } }; var otherInfoStream = new MemoryStream(); otherInfoSerializer.WriteObject(otherInfoStream, otherInfoList); otherInfoStream.Position = 0; //LI.OtherInfo = (new StreamReader(OtherInfoStream)).ReadToEnd(); var outInfo = new StringBuilder(); outInfo.Append("${auth_session}:").AppendLine(response.getAccessToken()); outInfo.Append("${auth_uuid}:").AppendLine(response.getSelectedProfile().getId()); outInfo.Append("${auth_access_token}:").AppendLine(response.getAccessToken()); if (response.getUser() != null) { if (response.getUser().getId() != null) { AuthenticationResponse.Properties[] properties = response.getUser().getProperties(); if (properties != null) { var propertiesObj = properties.ToDictionary(p => p.getName(), p => new[] { p.getValue() }); var pJsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string pJsonStr = pJsonSerializer.Serialize(propertiesObj); outInfo.Append("${user_properties}:").AppendLine(pJsonStr); } } } li.OtherInfo = li.SID; li.OutInfo = outInfo.ToString(); return(li); } catch (TimeoutException ex) { li.Suc = false; li.Errinfo = ex.Message; return(li); } }
public LoginInfo Login(string userName, string password, string clientIdentifier = "", string language = "zh-cn") { var li = new LoginInfo(); _clientToken = clientIdentifier; try { var auth = (HttpWebRequest)WebRequest.Create(RouteAuthenticate); auth.Method = "POST"; var ag = new AuthenticationRequest(userName, password); var agJsonSerialiaer = new DataContractJsonSerializer(typeof(AuthenticationRequest)); var agJsonStream = new MemoryStream(); agJsonSerialiaer.WriteObject(agJsonStream, ag); agJsonStream.Position = 0; string logindata = (new StreamReader(agJsonStream)).ReadToEnd(); byte[] postdata = Encoding.UTF8.GetBytes(logindata); auth.ContentLength = postdata.LongLength; Stream poststream = auth.GetRequestStream(); poststream.Write(postdata, 0, postdata.Length); poststream.Close(); var authans = (HttpWebResponse)auth.GetResponse(); var responseJsonSerializer = new DataContractJsonSerializer(typeof(AuthenticationResponse)); var res = authans.GetResponseStream(); if (res == null) { li.Suc = false; li.Errinfo = "服务器无响应"; return li; } var responseStream = new StreamReader(res); string responseJson = responseStream.ReadToEnd(); var responseJsonStream = new MemoryStream(Encoding.UTF8.GetBytes(responseJson)) {Position = 0}; var response = responseJsonSerializer.ReadObject(responseJsonStream) as AuthenticationResponse; if (response == null) { li.Suc = false; li.Errinfo = "服务器无响应"; return li; } if (response.getClientToken() != NewLogin._clientToken) { li.Suc = false; li.Errinfo = "客户端标识和服务器返回不符,这是个不常见的错误,就算是正版启动器这里也没做任何处理,只是报了这么个错。"; return li; } li.Suc = true; li.UN = response.getSelectedProfile().getName(); li.Client_identifier = NewLogin._clientToken; li.SID = response.getAccessToken(); var otherInfoSerializer = new DataContractJsonSerializer(typeof(SortedList)); var otherInfoList = new SortedList { {"${auth_uuid}", response.getSelectedProfile().getId()}, {"${auth_access_token}", response.getAccessToken()} }; var otherInfoStream = new MemoryStream(); otherInfoSerializer.WriteObject(otherInfoStream, otherInfoList); otherInfoStream.Position = 0; //LI.OtherInfo = (new StreamReader(OtherInfoStream)).ReadToEnd(); var outInfo = new StringBuilder(); outInfo.Append("${auth_session}:").AppendLine(response.getAccessToken()); outInfo.Append("${auth_uuid}:").AppendLine(response.getSelectedProfile().getId()); outInfo.Append("${auth_access_token}:").AppendLine(response.getAccessToken()); if (response.getUser() != null) { if (response.getUser().getId()!=null) { AuthenticationResponse.Properties[] properties = response.getUser().getProperties(); if (properties != null) { var propertiesObj = properties.ToDictionary(p => p.getName(), p => new[] {p.getValue()}); var pJsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string pJsonStr = pJsonSerializer.Serialize(propertiesObj); outInfo.Append("${user_properties}:").AppendLine(pJsonStr); } } } li.OtherInfo = li.SID; li.OutInfo = outInfo.ToString(); return li; } catch (TimeoutException ex) { li.Suc = false; li.Errinfo = ex.Message; return li; } }