private void ProcessIQ(IQ iq) { if (iq.Query.GetType() == typeof(Auth)) { Auth auth = iq.Query as Auth; switch (iq.Type) { case IqType.get: iq.SwitchDirection(); iq.Type = IqType.result; auth.AddChild(new Element("password")); auth.AddChild(new Element("digest")); Send(iq); break; case IqType.set: // Here we should verify the authentication credentials iq.SwitchDirection(); iq.Type = IqType.result; iq.Query = null; Send(iq); break; } } else if (iq.Query.GetType() == typeof(Roster)) { ProcessRosterIQ(iq); } }
public static void ProccessAuthGet(string username, int UserSessionIndex, IQ iq) { Auth auth = iq.Query as Auth; iq.SwitchDirection(); iq.Type = IqType.result; if (!auth.HasTag("username")) { auth.AddChild(new Element("username")); } auth.AddChild(new Element("digest")); auth.AddChild(new Element("resource")); ThreadTools.Users.Online[UserSessionIndex].Send(iq); }
private void ProcessIQ(IQ iq) { if (iq.Query.GetType() == typeof(Auth)) { Auth auth = iq.Query as Auth; switch (iq.Type) { //响应用户验证请求 case IqType.get: iq.SwitchDirection(); iq.Type = IqType.result; auth.AddChild(new Element("password")); auth.AddChild(new Element("digest")); Send(iq); break; //进行用户验证 case IqType.set: //认证完成后,将建立的socket连接保存到集合 OnlineUser.Users.Add(new User { UserName = auth.Username, UserId = 0, ConnSocket = m_Sock, Address = m_Sock.RemoteEndPoint.ToString() }); iq.SwitchDirection(); iq.Type = IqType.result; iq.Query = null; Send(iq); break; } } else if (iq.Query.GetType() == typeof(Roster)) { ProcessRosterIQ(iq); } }
/// <summary> /// 处理用户验证 /// </summary> /// <param name="sInfo"></param> private static void ProcessIQAuth(StreamInfo sInfo) { IQ iq = (IQ)sInfo.Node; Auth auth = (Auth)iq.Query; switch (iq.Type) { case IqType.get: iq.SwitchDirection(); iq.Type = IqType.result; auth.AddChild(new Element("password")); auth.AddChild(new Element("digest")); sInfo.Client.Send(iq); break; case IqType.set: User user = DataFactory.UserProvider.GetUserByUserName(auth.Username); //密码是MD5的大写 string digest = XMPPProtocol.Util.Hash.Sha1Hash(sInfo.Client.SessionID + user.UserPwd.ToUpper()); if (auth.Digest == digest && !string.IsNullOrEmpty(user.UserName)) //登录验证通过 { if (user.Status == 0) { iq.SwitchDirection(); iq.Type = IqType.error; iq.Query = null; iq.Error = new XMPPProtocol.Protocol.client.Error(); iq.Error.Message = "账户还没激活!"; sInfo.Client.Send(iq); } else if (user.Status == 2) { iq.SwitchDirection(); iq.Type = IqType.error; iq.Query = null; iq.Error = new XMPPProtocol.Protocol.client.Error(); iq.Error.Message = "账户被禁止登陆!"; sInfo.Client.Send(iq); } else { sInfo.Client.JID = new XMPPProtocol.Jid(auth.Username, IMServer.SERVERNAME, auth.Resource); //添加到全局客户端字典 ClientFactory.AddClient(sInfo.Client); //update 数据库 修改用户lastLoginIP lastLoginTime Server user.LastLoginIP = sInfo.Client.RemoteHostIP; user.Server = IMServer.SERVERNAME; DataFactory.UserProvider.UpdateUserLoginInfo(user); // sInfo.Client.User = user; //通知其他IMServer 逼在线的下线 //写入到登录日志 这里可以用log4net记录 //发送登录成功iq iq.SwitchDirection(); iq.Type = IqType.result; iq.Query = null; sInfo.Client.Send(iq); } } else { //验证失败,发送失败iq iq.SwitchDirection(); iq.Type = IqType.error; iq.Query = null; iq.Error = new XMPPProtocol.Protocol.client.Error(); iq.Error.Message = "用户名或密码错误!"; sInfo.Client.Send(iq); } break; } }
private void ProcessIQAsync(agsXMPP.XmppSeverConnection contextConnection, IQ iq) { if (iq.Query.GetType() == typeof(Auth)) { Auth auth = iq.Query as Auth; string name = (auth.Username); string resource = auth.Resource; if (resource == null) { resource = ""; } var user = new FoxundermoonLib.XmppEx.Data.User(name, resource); switch (iq.Type) { case IqType.get: iq.SwitchDirection(); iq.Type = IqType.result; auth.AddChild(new Element("password")); //auth.AddChild(new Element("digest")); Console.WriteLine(auth.Username + " :开始登陆!"); contextConnection.Send(iq); break; case IqType.set: // Here we should verify the authentication credentials Console.WriteLine(auth.Username + " : " + "开始验证, 密码:" + auth.Password); iq.SwitchDirection(); if (AccountBus.CheckAccountAsync(auth.Username, auth.Password)) //验证用户是否存在或者密码是否正确 { contextConnection.IsAuthentic = true; iq.Type = IqType.result; iq.Query = null; try { ConcurrentDictionary <string, XmppSeverConnection> cons = null; //Func<int,XmppSeverConnection,XmppSeverConnection> update = (k,v)=>{return v;}; //XmppConnectionDic.AddOrUpdate(uid, contextConnection),(k,v)=>{return v;}); var hasCons = XmppConnectionDic.TryGetValue(name, out cons); if (hasCons) { XmppSeverConnection con = null; var hasCon = cons.TryGetValue(resource, out con); if (hasCon) { cons.TryRemove(resource, out con); Console.WriteLine(name + " 重新登录"); try { //con.Stop(); } catch (Exception e) { Console.WriteLine("[email protected] old connection :" + e.Message); } } } if (!hasCons) { cons = new ConcurrentDictionary <string, XmppSeverConnection>(); if (XmppConnectionDic.TryAdd(name, cons)) { Console.WriteLine(auth.Username + ": 账号验证成功,并加入连接池!"); } else { Console.WriteLine(auth.Username + ": 账号验证成功,但是加入连接池失败!"); } } cons.TryAdd(resource, contextConnection); contextConnection.User = user; UserOnline(user); } catch (Exception e) { // 消息没有 From dosomething iq.Type = IqType.error; iq.Value = e.Message; Console.WriteLine("Exception --> message: " + e.Message + " data:" + e.Data); } } else { // authorize failed iq.Type = IqType.error; //若要开启验证功能去掉此注释 Console.WriteLine(auth.Username + ":账号验证失败!"); FoxundermoonLib.XmppEx.Data.Message loginFailed = new FoxundermoonLib.XmppEx.Data.Message(); loginFailed.Command.Name = FoxundermoonLib.XmppEx.Command.Cmd.ErrorMessage; loginFailed.AddProperty("Cause", "账号验证失败,请检查用户名或者密码"); loginFailed.ToUser = user; UniCast(loginFailed); //iq.Type = IqType.result; iq.Query = null; iq.Value = "authorized failed"; contextConnection.IsAuthentic = false; } try { contextConnection.Send(iq); } catch (Exception e) { Console.WriteLine("Exception->@IQhandler.processIq:" + e.Message); } break; } } else if (!contextConnection.IsAuthentic) { contextConnection.Stop(); } else if (iq.Query.GetType() == typeof(Roster)) { ProcessRosterIQ(contextConnection, iq); } }