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 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; }
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; } }
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); } }
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); } }
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); } } }