protected void ddPendingUsers_OnSelectedIndexChanged(object sender, EventArgs e) { if (ddPendingUsers.SelectedValue == Guid.Empty.ToString()) { hiddenPendingUser.Value = Guid.Empty.ToString(); clearAllCreateFields(); return; } hiddenPendingUser.Value = ddPendingUsers.SelectedValue; try { Database.Interface.open(); Database.PendingAccount pendingAccount = new Database.PendingAccount(); pendingAccount.select_all_where_ID(new Guid(ddPendingUsers.SelectedValue)); tbName.Text = pendingAccount.Name; tbContact.Text = pendingAccount.Contact; tbAddress.Text = pendingAccount.Address; tbPostal.Text = pendingAccount.Postal; tbEmail.Text = pendingAccount.Email; tbPhone.Text = pendingAccount.Phone; tbMobile.Text = pendingAccount.Mobile; tbFax.Text = pendingAccount.Fax; tbWebsite.Text = pendingAccount.Website; } catch (Exception ex) { Utils.displayStatus(ref labelStatusCreate, Color.Red, ex.Message); } finally { Database.Interface.close(); } }
protected void buttonCreateAccount_OnClick(object sender, EventArgs e) { bool accountCreated = false; Database.Account account = null; if (String.IsNullOrEmpty(tbUserName.Text) || String.IsNullOrEmpty(tbPassword.Text) || String.IsNullOrEmpty(tbEmail.Text) || String.IsNullOrEmpty(tbName.Text) || String.IsNullOrEmpty(tbAddress.Text) || String.IsNullOrEmpty(tbPostal.Text)) { Utils.displayStatus(ref labelStatusCreate, Color.Red, "Mangler informasjon"); return; } if (!Utils.isValidEmail(tbEmail.Text)) { Utils.displayStatus(ref labelStatusCreate, Color.Red, "Epost adresse har ugyldig format"); return; } if (tbPassword.Text.Length < Membership.MinRequiredPasswordLength) { Utils.displayStatus(ref labelStatusCreate, Color.Red, "Passordet må ha minst " + Membership.MinRequiredPasswordLength.ToString() + " tegn"); return; } if (tbPassword.Text != tbConfirmPassword.Text) { Utils.displayStatus(ref labelStatusCreate, Color.Red, "Passordene er ikke like"); return; } if (String.IsNullOrEmpty(ConfigurationManager.AppSettings["MailServer"]) || String.IsNullOrEmpty(ConfigurationManager.AppSettings["MailServerPort"])) { Utils.displayStatus(ref labelStatusCreate, Color.Red, "Innstillinger for mailserver mangler"); return; } Database.Configuration configuration = new Database.Configuration(); try { Membership.ApplicationName = "/Lorakon"; Database.Interface.open(); if (!configuration.select_all_where_name("Default")) { Utils.displayStatus(ref labelStatusCreate, Color.Red, "Finner ikke konfigurasjon"); return; } if (Database.Account.accountNameExists(tbName.Text)) { Utils.displayStatus(ref labelStatusCreate, Color.Red, "Navnet " + tbName.Text + " finnes allerede"); Membership.ApplicationName = "/LorakonAdmin"; return; } MembershipCreateStatus status = new MembershipCreateStatus(); MembershipUser user = Membership.CreateUser(tbUserName.Text, tbPassword.Text, tbEmail.Text, "question", "answer", true, out status); if (user == null) { Utils.displayStatus(ref labelStatusCreate, Color.Red, Utils.getErrorMessage(status)); Membership.ApplicationName = "/LorakonAdmin"; return; } account = new Database.Account( Guid.Empty, tbName.Text, tbContact.Text, tbAddress.Text, "Field not active", tbPostal.Text, tbEmail.Text, tbPhone.Text, tbMobile.Text, tbFax.Text, tbWebsite.Text, true, "", 0, 0, ""); accountCreated = account.insert_with_ID((Guid)user.ProviderUserKey); if (!String.IsNullOrEmpty(hiddenPendingUser.Value) && hiddenPendingUser.Value != Guid.Empty.ToString()) { Database.PendingAccount pendingAccount = new Database.PendingAccount(); if (pendingAccount.select_all_where_ID(new Guid(hiddenPendingUser.Value))) { pendingAccount.delete_by_ID(); } } ddUsers.DataBind(); ddAccountsA.DataBind(); } catch (Exception ex) { Membership.DeleteUser(tbUserName.Text); if (accountCreated) { account.delete_by_ID(); } Utils.displayStatus(ref labelStatusCreate, Color.Red, ex.Message); return; } finally { Database.Interface.close(); Membership.ApplicationName = "/LorakonAdmin"; } string userName = tbUserName.Text; string receip = tbEmail.Text; string mailTitle = "Ny LORAKON konto tildelt"; string mailBody = @"Velkommen som bruker av LORAKON nettjenester.<br> Hver bedrift får kun tildelt ett brukernavn og passord. Dersom bedriften har flere ansatte som skal være delaktige i nettverket må brukernavnet og passordet deles mellom disse.<br> Deres bedrift har fått tildelt følgende brukernavn: " + tbUserName.Text + " og passord: " + tbPassword.Text + @".<br> For å logge inn på sidene kan følgende lenke benyttes: <a href='" + ConfigurationManager.AppSettings["LorakonURL"] + "'>" + ConfigurationManager.AppSettings["LorakonURL"] + @"</a><br> NB! Brukernavnet er låst, men brukeren kan selv endre passordet ved behov via siden 'Bedriftens konto'<br><br> Hilsen Statens Strålevern"; clearAllCreateFields(); ddPendingUsers.DataBind(); ddUsers.DataBind(); ddAccountsA.DataBind(); if (ConfigurationManager.AppSettings["UseEmail"] == "yes") { try { MailMessage mail = new MailMessage(); mail.To.Add(receip); mail.From = new MailAddress(configuration.RingtestAdminEmail); mail.IsBodyHtml = true; mail.BodyEncoding = System.Text.Encoding.Default; mail.Subject = mailTitle; mail.Body = mailBody; SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["MailServer"], Convert.ToInt32(ConfigurationManager.AppSettings["MailServerPort"])); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(mail); Utils.displayStatus(ref labelStatusCreate, Color.SeaGreen, "Bruker " + userName + " ble opprettet, og e-post er sendt til " + receip); } catch (Exception ex) { Utils.displayStatus(ref labelStatusCreate, Color.Red, ex.Message); } } }