public override bool DeleteUser(string username, bool deleteAllRelatedData) { if (string.IsNullOrEmpty(username)) { throw new ArgumentNullException("username"); } string tenantName; string tenantUsername; if (!Upn.TryParse(username, out tenantName, out tenantUsername)) { return(false); //invalid username format } try { if (!AuthenticationProxy.UserExists(tenantName, tenantUsername)) { return(false); //not exists } AuthenticationProxy.DeleteUser(tenantName, tenantUsername); } catch (Exception ex) { throw new ProviderException(ex.Message, ex); } return(true); }
protected void chbRoles_CheckedChanged(object sender, EventArgs e) { string membershipUserName = new Upn(TenantName, Request["username"]).ToString(); if (string.IsNullOrEmpty(membershipUserName)) { membershipUserName = Membership.GetUser().UserName; } CheckBox chkBox = sender as CheckBox; string roleName = chkBox.Text; if (chkBox.Checked) { if (!Roles.IsUserInRole(membershipUserName, roleName)) { Roles.AddUserToRole(membershipUserName, roleName); } } else { if (Roles.IsUserInRole(membershipUserName, roleName)) { Roles.RemoveUserFromRole(membershipUserName, roleName); } } }
protected void btnSaveButton_ServerClick(object sender, EventArgs e) { if (!HasPriv()) { return; } try { string tenantUserName = new Upn(CurrentTenant.Name, txtUserID.Text.Trim()).ToString(); MembershipUser user = Membership.GetUser(tenantUserName); if (user.Email != txtEmail.Text.Trim()) { user.Email = txtEmail.Text.Trim(); Membership.UpdateUser(user); } ProfileCommon commProfile = Profile.GetProfile(tenantUserName); commProfile.FullName = txtFullName.Text.Trim(); commProfile.Title = txtTitle.Text.Trim(); commProfile.Sex = dplSex.Text.Trim(); commProfile.Birthday = txtBirthday.Text.Trim(); commProfile.Telephone = txtTel.Text.Trim(); commProfile.Fax = txtFax.Text.Trim(); commProfile.MsnAccount = txtMsn.Text.Trim(); commProfile.Address = txtAddress.Text.Trim(); commProfile.ZipCode = txtZipCode.Text.Trim(); commProfile.Mobile = txtMobile.Text.Trim(); commProfile.Save(); lblErrMsg.Text = Resources.GlobalResources.SuccessSave; } catch (Exception) { lblErrMsg.Text = Resources.GlobalResources.FailSave; } }
protected void chbRole_CheckedChanged(object sender, EventArgs e) { string membershipUserName = new Upn(TenantName, SelectedUsername).ToString(); CheckBox chkBox = sender as CheckBox; string roleName = chkBox.Text; if (chkBox.Checked) { if (!Roles.IsUserInRole(membershipUserName, roleName)) { Roles.AddUserToRole(membershipUserName, roleName); } } else { if (Roles.IsUserInRole(membershipUserName, roleName)) { if (string.Compare(roleName, CrabApp.TenantRoles.Administrators.ToString(), true) == 0) { lblError.Text = string.Format("The admin user could not remove the role Administrators by himself/herself."); return; } Roles.RemoveUserFromRole(membershipUserName, roleName); } } }
/// <summary> /// Bind repeater and check the user in role /// </summary> /// <param name="rp"></param> /// <param name="user"></param> private void BindRepeater(Repeater rp, string username) { string[] roles = Roles.GetAllRoles(); IList <string> newRoles = new List <string>(); Upn upn = new Upn(username); foreach (string role in roles) { if (role.ToLower() == CrabApp.TenantRoles.SysAdmin.ToString().ToLower()) { continue; } else if (role.ToLower() == CrabApp.TenantRoles.Administrators.ToString().ToLower() && upn.Username.ToLower() == base.Username.ToLower()) { continue; } newRoles.Add(role); } rp.DataSource = newRoles; rp.DataBind(); for (int i = 0; i < rp.Items.Count; i++) { CheckBox ck = (CheckBox)rp.Items[i].FindControl("chbRoles"); bool IsChked = Roles.IsUserInRole(username, ck.Text.ToString()); if (IsChked) { ck.Checked = true; } } }
public override void AddUsersToRoles(string[] usernames, string[] roleNames) { Upn upn = new Upn(HttpContext.Current.User.Identity.Name); string[] adamUsernames = FromUpns(usernames); AuthorizationProxy.AddUsersToRoles(upn.TenantName, adamUsernames, roleNames); }
protected void Page_Load(object sender, EventArgs e) { if (!HasPriv()) { lblErrMsg.Text = Resources.GlobalResources.NoPrivilege; return; } if (!IsPostBack) { string username = Request["username"]; string returnUrl = Request["ReturnUrl"]; ViewState["UserName"] = username; ViewState["ReturnUrl"] = returnUrl; MembershipUser user = null; user = string.IsNullOrEmpty(username) ? Membership.GetUser() : Membership.GetUser((new Upn(base.TenantName, username)).ToString()); if (user == null) { lblErrMsg.Text = string.Format(Resources.GlobalResources.UserNotExists, username); return; } Upn upn = new Upn(user.UserName); txtUserID.Text = upn.Username; GetUserInfo(user.UserName); BindRepeater(RepeaterRoles, user.UserName); } }
/// <summary> /// Returns true if ProvisioningRequestRequestedMembers instances are equal /// </summary> /// <param name="other">Instance of ProvisioningRequestRequestedMembers to be compared</param> /// <returns>Boolean</returns> public bool Equals(ProvisioningRequestRequestedMembers other) { if (other is null) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return (( Id == other.Id || Id != null && Id.Equals(other.Id) ) && ( Upn == other.Upn || Upn != null && Upn.Equals(other.Upn) ) && ( Email == other.Email || Email != null && Email.Equals(other.Email) )); }
public override string[] GetRolesForUser(string username) { string tenantName = null; string tenantUsername = null; Upn.TryParse(username, out tenantName, out tenantUsername); return(AuthorizationProxy.GetRolesForUser(tenantName, tenantUsername)); }
public override bool IsUserInRole(string username, string roleName) { string tenantName = null; string tenantUsername = null; Upn.TryParse(username, out tenantName, out tenantUsername); return(AuthorizationProxy.IsUserInRole(tenantName, tenantUsername, roleName)); }
public override bool ValidateUser(string username, string password) { string tenantName; string tenantUsername; if (!Upn.TryParse(username, out tenantName, out tenantUsername)) { return(false); } return(AuthenticationProxy.ValidateUser(tenantName, tenantUsername, password)); }
static private bool UserExistsInDb(string tenantName, string username) { Database db = DatabaseFactory.CreateDatabase(Constants.Database.TenantIdentity); using (DbCommand command = db.GetSqlStringCommand(SQL_CHECK_EXISTENCE)) { Upn upn = new Upn(tenantName, username); db.AddInParameter(command, "Upn", DbType.String, upn.ToString()); return((int)db.ExecuteScalar(command) > 0); } }
public override void CreateRole(string roleName) { string tenantName = null; string tenantUsername = null; Upn.TryParse(HttpContext.Current.User.Identity.Name, out tenantName, out tenantUsername); if (!string.IsNullOrEmpty(tenantName)) { AuthorizationProxy.CreateRole(tenantName, roleName); } }
private string[] FromUpns(string[] upns) { string tenantName = null; string[] usernames = new string[upns.Length]; for (int i = 0; i < upns.Length; i++) { Upn.TryParse(upns[i], out tenantName, out usernames[i]); } return(usernames); }
protected void btnCreateUser_Click(object sender, EventArgs e) { try { List <string> roles = new List <string>(); cbl = ((CheckBoxList)this.CreateUserWizardStep1.ContentTemplateContainer.FindControl("cblBoundRoles")); bool roleSelected = false; for (int i = 0; i < cbl.Items.Count; i++) { if (cbl.Items[i].Selected) { roles.Add(cbl.SelectedValue.ToString()); roleSelected = true; } } lblReturnMessage.Text = string.Empty; if (Membership.GetAllUsers().Count >= CurrentTenant.LicenseCount) { lblReturnMessage.Text = Resources.GlobalResources.OutOfUserLicense; return; } Upn upn = new Upn(TenantName, CreateUserWizard1.UserName.Trim()); MembershipCreateStatus createStatus; MembershipUser user = Membership.CreateUser(upn.ToString(), CreateUserWizard1.Password, CreateUserWizard1.Email, null, null, true, out createStatus); if (user == null) { switch (createStatus) { case MembershipCreateStatus.DuplicateUserName: this.lblReturnMessage.Text = string.Format(Resources.GlobalResources.ExistsUser, CreateUserWizard1.UserName); break; case MembershipCreateStatus.InvalidUserName: this.lblReturnMessage.Text = Resources.GlobalResources.InvalidUsername; break; default: lblReturnMessage.Text = Resources.GlobalResources.FailCreateUser; break; } return; } CreateUserWizard1.ActiveStepIndex = 1; } catch (Exception ex) { lblReturnMessage.Text = ex.Message; } }
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { string tenantName = null; string tenantUsername = null; Upn.TryParse(HttpContext.Current.User.Identity.Name, out tenantName, out tenantUsername); if (!string.IsNullOrEmpty(tenantName)) { AuthorizationProxy.DeleteRole(tenantName, roleName); } return(true); }
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { string tenantName = null; string tenantUsername = null; Upn.TryParse(HttpContext.Current.User.Identity.Name, out tenantName, out tenantUsername); string[] adamUsernames = FromUpns(usernames); if (!string.IsNullOrEmpty(tenantName)) { AuthorizationProxy.RemoveUsersFromRoles(tenantName, adamUsernames, roleNames); } }
static private void CreateUserInDb(string tenantName, string username) { Tenant tenant = ProvisionLogic.GetTenantByName(tenantName); Database db = DatabaseFactory.CreateDatabase(Constants.Database.TenantIdentity); using (DbCommand command = db.GetSqlStringCommand(SQL_CREATE_USER_IN_DB)) { Upn upn = new Upn(tenantName, username); db.AddInParameter(command, "Id", DbType.Guid, Guid.NewGuid()); db.AddInParameter(command, "TenantId", DbType.Guid, tenant.Id); db.AddInParameter(command, "Upn", DbType.String, upn.ToString().ToLower()); db.ExecuteNonQuery(command); } }
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { string tenantName = ((TextBox)Login1.FindControl("TenantName")).Text.Trim(); string tenantUsername = Login1.UserName.Trim(); Upn upn = new Upn(tenantName, tenantUsername); bool authenticated = false; if (!(authenticated = Membership.ValidateUser(upn.ToString(), Login1.Password))) { Login1.FailureText = Resources.GlobalResources.FailLogin; return; } Login1.UserName = upn.ToString(); e.Authenticated = authenticated; // http://codeverge.com/asp.net.web-forms/asp-login-remember-me-functionality/390002 HttpCookie myCookie = new HttpCookie("myCookie"); Boolean remember = Login1.RememberMeSet; if (remember) { Int32 persistDays = 15; myCookie.Values.Add("username", tenantUsername); myCookie.Values.Add("tenantname", tenantName); myCookie.Expires = DateTime.Now.AddDays(persistDays); //you can add years and months too here } else { myCookie.Values.Add("username", string.Empty); // overwrite empty string is safest myCookie.Values.Add("tenantname", string.Empty); myCookie.Expires = DateTime.Now.AddMinutes(5); //you can add years and months too here } Response.Cookies.Add(myCookie); /* * if (User.Identity.IsAuthenticated) * { * FormsIdentity identity = User.Identity as FormsIdentity; * FormsAuthenticationTicket ticket = identity.Ticket; * if (ticket.IsPersistent) * { * Login1.RememberMeSet = true; * } * } */ //RememberMe(); }
protected void ManagerUsers_RowCommand(object sender, GridViewCommandEventArgs e) { SelectedUsername = e.CommandArgument.ToString(); string membershipUserName = new Upn(TenantName, SelectedUsername).ToString(); GvRoles.Visible = false; if (e.CommandName == "DeleteUser") { if (SelectedUsername.ToLower() == base.Username.ToLower()) { lblErrMsg.Text = Resources.GlobalResources.CannotDelMyself; return; } bool result = Membership.DeleteUser(new Upn(TenantName, SelectedUsername).ToString(), true); if (result == true) { BindGrid(this.ManagerUsers, PageIndex); } } if (e.CommandName == "EditUser") { string returnUrl = Request.RawUrl; Response.Redirect("~/Admin/Users/EditUser.aspx?Username="******"&ReturnUrl=" + returnUrl); } if (e.CommandName == "EditRole") { //Bind Role GvRoles.Visible = true; BindGvRole(this.GvRoles); string[] roles = Roles.GetRolesForUser(membershipUserName); for (int i = 0; i < GvRoles.Rows.Count; i++) { CheckBox chb = (CheckBox)GvRoles.Rows[i].Cells[0].FindControl("chbRole"); chb.Checked = false; } for (int i = 0; i < GvRoles.Rows.Count; i++) { CheckBox chb = (CheckBox)GvRoles.Rows[i].Cells[0].FindControl("chbRole"); for (int j = 0; j < roles.Length; j++) { if (chb.Text.ToLower() == roles[j].ToLower()) { chb.Checked = true; } } } } }
public override string[] GetAllRoles() { string tenantName = null; string tenantUsername = null; Upn.TryParse(HttpContext.Current.User.Identity.Name, out tenantName, out tenantUsername); if (!string.IsNullOrEmpty(tenantName)) { return(AuthorizationProxy.GetAllRoles(tenantName)); } else { return(null); } }
public override bool RoleExists(string roleName) { string tenantName = null; string tenantUsername = null; Upn.TryParse(HttpContext.Current.User.Identity.Name, out tenantName, out tenantUsername); if (!string.IsNullOrEmpty(tenantName)) { return(AuthorizationProxy.RoleExists(tenantName, roleName)); } else { return(false); } }
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { string tenantName = ((TextBox)Login1.FindControl("TenantName")).Text.Trim(); string tenantUsername = Login1.UserName.Trim(); Upn upn = new Upn(tenantName, tenantUsername); bool authenticated = false; if (!(authenticated = Membership.ValidateUser(upn.ToString(), Login1.Password))) { Login1.FailureText = Resources.GlobalResources.FailLogin; return; } Login1.UserName = upn.ToString(); e.Authenticated = authenticated; }
static private Guid GetUserIdFromDb(string tenantName, string username) { Database db = DatabaseFactory.CreateDatabase(Constants.Database.TenantIdentity); using (DbCommand command = db.GetSqlStringCommand(SQL_GET_USER_ID_FROM_DB)) { Upn upn = new Upn(tenantName, username); db.AddInParameter(command, "Upn", DbType.String, upn.ToString()); object result = db.ExecuteScalar(command); if (result == null || result is DBNull) { return(Guid.Empty); } else { return((Guid)result); } } }
public override MembershipUser GetUser(string username, bool userIsOnline) { string tenantName; string tenantUsername; if (!Upn.TryParse(username, out tenantName, out tenantUsername)) { throw new ArgumentException(string.Format("The username {0} is invalid!", username)); } AdamUser adamUser = AuthenticationProxy.GetAdamUser(tenantName, tenantUsername); if (adamUser == null) { return(null); } object providerUserKey = new SecurityIdentifier(adamUser.Sid, 0); return(new ActiveDirectoryMembershipUser(this.Name, adamUser.PrincipleName, providerUserKey, adamUser.Email, string.Empty, string.Empty , true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now)); }
/// <summary> /// Gets the hash code /// </summary> /// <returns>Hash code</returns> public override int GetHashCode() { unchecked // Overflow is fine, just wrap { var hashCode = 41; // Suitable nullity checks etc, of course :) if (Id != null) { hashCode = hashCode * 59 + Id.GetHashCode(); } if (Upn != null) { hashCode = hashCode * 59 + Upn.GetHashCode(); } if (Email != null) { hashCode = hashCode * 59 + Email.GetHashCode(); } return(hashCode); } }
public override bool ChangePassword(string username, string oldPassword, string newPassword) { if (string.IsNullOrEmpty(username)) { throw new ArgumentNullException("username"); } string tenantName; string tenantUsername; if (!Upn.TryParse(username, out tenantName, out tenantUsername)) { return(false); } CheckPassword(oldPassword, MinRequiredPasswordLength, "oldPassword"); CheckPassword(oldPassword, MinRequiredPasswordLength, "newPassword"); if (newPassword.Length < this.MinRequiredPasswordLength) { throw new ArgumentException(string.Format("The length of the passowrd {0} must not be shorter than {1} characters!", "newPassword", MinRequiredPasswordLength)); } int num1 = 0; for (int num2 = 0; num2 < newPassword.Length; num2++) { if (!char.IsLetterOrDigit(newPassword, num2)) { num1++; } } if (num1 < this.MinRequiredNonAlphanumericCharacters) { throw new ArgumentException(string.Format("Non alpha numeric characters of {0} must be more than {1}", "newPassword", MinRequiredNonAlphanumericCharacters)); } if ((this.PasswordStrengthRegularExpression.Length > 0) && !Regex.IsMatch(newPassword, this.PasswordStrengthRegularExpression)) { throw new ArgumentException(string.Format("The format of the password {0} is not correct!", "newPassword")); } AuthenticationProxy.ChangePassword(tenantName, tenantUsername, oldPassword, newPassword); return(true); }
public override ReadOnlySpan <byte> Marshal() { var buffer = new NdrBuffer(); var upnBytes = MemoryMarshal.Cast <char, byte>(Upn.AsSpan()); var domainBytes = MemoryMarshal.Cast <char, byte>(Domain.AsSpan()); buffer.WriteInt16LittleEndian((short)upnBytes.Length); buffer.WriteInt16LittleEndian(2 + 2 + 2 + 2 + 4 + 4); // + 4 to align on 8 boundary buffer.WriteInt16LittleEndian((short)domainBytes.Length); buffer.WriteInt16LittleEndian((short)(2 + 2 + 2 + 2 + 2 + 4 + 4 + upnBytes.Length)); buffer.WriteInt32LittleEndian((int)Flags); buffer.WriteInt32LittleEndian(0); buffer.WriteFixedPrimitiveArray(upnBytes); buffer.WriteInt16LittleEndian(0); buffer.WriteFixedPrimitiveArray(domainBytes); return(buffer.ToSpan(alignment: 8)); }
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) { //extract tenant name from the current user upn string tenantName; string username; Upn.TryParse(HttpContext.Current.User.Identity.Name, out tenantName, out username); MembershipUserCollection collection = new MembershipUserCollection(); string[] usernames = AuthenticationProxy.GetUsers(tenantName); totalRecords = usernames.Length; int start = pageIndex * pageSize; if (start >= 0 && start < totalRecords) { for (int i = start; i < totalRecords && i < start + pageSize; i++) { collection.Add(System.Web.Security.Membership.GetUser((new Upn(tenantName, usernames[i])).ToString())); } } return(collection); }
protected string ExtractUsername(string upn) { Upn principle = new Upn(upn); return(principle.Username); }