public void ApiUsersGetID_Test() { var client = new DiscuzApiClient(); var responseResult = client.UsersGetID("cyrano"); Assert.AreEqual("\"443\"", responseResult); }
public void ApiAuthValidate_Test() { var client = new DiscuzApiClient(); var uid = client.AuthValidate("cyrano", Encrypt.GetMd5Hash("linfeng")); Assert.AreEqual(443, Convert.ToInt32(uid.Replace("\"", ""))); }
public void ApiUsersGetInfo_Test() { var client = new DiscuzApiClient(); int[] uids = { 443, 17650 }; string[] fields = { "uid", "user_name", "password", "email", "mobile", "join_date" }; var responseResult = client.UsersGetInfo(uids, fields); Assert.IsNotNull(responseResult); }
// // Summary: // Verifies that the supplied user name and password are valid. // // Parameters: // username: // The name of the user to be validated. // // password: // The password for the specified user. // // Returns: // true if the supplied user name and password are valid; otherwise, false. //public static bool ValidateUser(string username, string password, out object providerUserKey) //{ // Contract.Requires(!string.IsNullOrEmpty(username)); // Contract.Requires(!string.IsNullOrEmpty(password)); // IRepository repo = new Repository(); // var query = repo.Query<Membership>(x => // x.UserName == username && x.Password == Encrypt.GetMd5Hash(password)); // if (query.Count > 0) // { // var membership = query[0]; // providerUserKey = membership.ID; // membership.LastLoginDate = DateTime.Now; // repo.Update(membership); // } // else // { // providerUserKey = null; // } // return query.Count > 0; //} public static bool ValidateAcnUser(string username, string password, out int acnUid) { acnUid = -1; if (!ConfigGlobal_Arsenal.AcnSync) { return(false); } var client = new DiscuzApiClient(); var uid = client.AuthValidate(username, Encrypt.GetMd5Hash(password)); return(int.TryParse(uid.Replace("\"", ""), out acnUid)); }
public static int GetAcnId(string username) { if (!ConfigGlobal_Arsenal.AcnSync) { return(-1); } var client = new DiscuzApiClient(); var uid = client.UsersGetID(username); int acnUid; if (int.TryParse(uid.Replace("\"", ""), out acnUid)) { return(acnUid); } return(0); }
// Summary: // Updates the password for the membership user in the membership data store. // // Parameters: // oldPassword: // The current password for the membership user. // // newPassword: // The new password for the membership user. // // Returns: // true if the update was successful; otherwise, false. // // Exceptions: // System.ArgumentException: // oldPassword is an empty string.-or-newPassword is an empty string. // // System.ArgumentNullException: // oldPassword is null.-or-newPassword is null. // // System.PlatformNotSupportedException: // This method is not available. This can occur if the application targets the // .NET Framework 4 Client Profile. To prevent this exception, override the // method, or change the application to target the full version of the .NET // Framework. public static bool ChangePassword(Membership instance, string oldPassword, string newPassword) { Contract.Requires(!string.IsNullOrEmpty(oldPassword)); Contract.Requires(!string.IsNullOrEmpty(newPassword)); if (oldPassword.Equals(newPassword, StringComparison.OrdinalIgnoreCase)) { throw new Exception("新密码应与旧密码不同"); } if (!instance.Password.Equals(Encrypt.GetMd5Hash(oldPassword))) { throw new Exception("用户旧密码验证不正确"); } using (var conn = new SqlConnection(DataAccess.ConnectString)) { conn.Open(); var trans = conn.BeginTransaction(); try { IRepository repo = new Repository(); instance.Password = Encrypt.GetMd5Hash(newPassword); instance.LastPasswordChangedDate = DateTime.Now; repo.Update(instance, trans); #region Sync Acn User Password if (ConfigGlobal_Arsenal.AcnSync) { var user = repo.Single <User>(instance.ID); if (user?.AcnID != null) { var client = new DiscuzApiClient(); var result = client.UsersChangePassword(user.AcnID.Value, Encrypt.GetMd5Hash(oldPassword), Encrypt.GetMd5Hash(newPassword)); if (!Convert.ToBoolean(result.Replace("\"", ""))) { throw new Exception("ACN同步失败"); } } } #endregion trans.Commit(); return(true); } catch { trans.Rollback(); throw; } } }
// Summary: // Adds a new user to the data store. // // Parameters: // username: // The user name for the new user. // // password: // The password for the new user. // // Returns: // A System.Web.Security.MembershipUser object for the newly created user. // // Exceptions: // System.Web.Security.MembershipCreateUserException: // The user was not created. Check the System.Web.Security.MembershipCreateUserException.StatusCode // property for a System.Web.Security.MembershipCreateStatus value. public void CreateUser(string username, string email, string password, out object providerUserKey, out MembershipCreateStatus status) { using (var conn = new SqlConnection(DataAccess.ConnectString)) { conn.Open(); var trans = conn.BeginTransaction(); try { IRepository repo = new Repository(); providerUserKey = null; Init(); #region Check username if (string.IsNullOrEmpty(username)) { status = MembershipCreateStatus.InvalidUserName; return; } if (string.IsNullOrEmpty(email)) { status = MembershipCreateStatus.InvalidEmail; return; } if (ConfigGlobal_Arsenal.AcnSync && GetAcnId(username) > 0) { status = MembershipCreateStatus.DuplicateUserName; return; } if (Single(username) != null) { status = MembershipCreateStatus.DuplicateUserName; return; } UserName = username; #endregion Password = Encrypt.GetMd5Hash(password); Mobile = string.Empty; Email = email; repo.Insert <Membership>(this, out providerUserKey, trans); #region Check user in the data store if (repo.Single <User>(providerUserKey) != null) { status = MembershipCreateStatus.DuplicateProviderUserKey; return; } #endregion var user = new User(); user.ID = (Guid)providerUserKey; user.UserName = UserName; user.IsAnonymous = false; user.LastActivityDate = DateTime.Now; #region Register new Acn User if (ConfigGlobal_Arsenal.AcnSync) { var client = new DiscuzApiClient(); var uid = client.AuthRegister(UserName, Password, Email); user.AcnID = Convert.ToInt32(uid.Replace("\"", "")); user.AcnUserName = UserName; } else { user.AcnID = null; user.AcnUserName = string.Empty; } #endregion user.MemberID = null; user.MemberName = string.Empty; user.WeChatOpenID = null; user.WeChatNickName = string.Empty; repo.Insert(user, trans); trans.Commit(); status = MembershipCreateStatus.Success; } catch (Exception ex) { trans.Rollback(); _log.Error(ex, new LogInfo { MethodInstance = MethodBase.GetCurrentMethod(), ThreadInstance = Thread.CurrentThread }); providerUserKey = null; status = MembershipCreateStatus.ProviderError; } } }
// Summary: // Adds a new user to the data store by exist Acn user. // public void CreateAcnUser(int uid, out MembershipCreateStatus status) { status = MembershipCreateStatus.UserRejected; if (!ConfigGlobal_Arsenal.AcnSync) { return; } #region Get Acn UserInfo to init the intance of MembershipDto var client = new DiscuzApiClient(); int[] uids = { uid }; string[] fields = { "user_name", "password", "email", "mobile", "join_date" }; var responseResult = client.UsersGetInfo(uids, fields); if (string.IsNullOrEmpty(responseResult)) { return; } var jlist = JArray.Parse(responseResult); var json = jlist[0]; Init(); UserName = json["user_name"].ToString(); Password = json["password"].ToString(); Mobile = json["mobile"].ToString(); Email = json["email"].ToString(); CreateDate = Convert.ToDateTime(json["join_date"].ToString()); Remark = $"{{\"AcnID\": {uid}}}"; #endregion using (var conn = new SqlConnection(DataAccess.ConnectString)) { conn.Open(); var trans = conn.BeginTransaction(); try { IRepository repo = new Repository(); object providerUserKey; repo.Insert <Membership>(this, out providerUserKey, trans); var user = new User { ID = (Guid)providerUserKey, UserName = UserName, IsAnonymous = false, LastActivityDate = DateTime.Now, AcnID = uid, AcnUserName = UserName, MemberID = null, MemberName = string.Empty, WeChatOpenID = null, WeChatNickName = string.Empty }; repo.Insert(user, trans); trans.Commit(); status = MembershipCreateStatus.Success; } catch { trans.Rollback(); status = MembershipCreateStatus.ProviderError; } } }