public async static Task<UserProfile> GetUserProfileTask(string UserName) { UserProfile response = new UserProfile(); StringBuilder SqlStatement = new StringBuilder(); //SQL Statement ============================================================= SqlStatement.Append("SELECT * FROM UserProfile WHERE UserName = '******'"); SqlCommand sqlCommand = new SqlCommand(SqlStatement.ToString(), new SqlConnection(EnvironmentSettings.SqlConnectionString)); sqlCommand.Connection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader(); while (reader.Read()) { response.Company = reader["Company"].ToString(); response.Email = reader["Email"].ToString(); response.UserID = Convert.ToInt32(reader["UserID"].ToString()); response.UserName = reader["UserName"].ToString(); } sqlCommand.Connection.Close(); return response; }
public async Task<bool> LogAsync(UserProfile userProfile, string logType, string logSubType, string description, HttpRequestBase request) { LogItem logItem = new LogItem(); logItem.LogType = logType; logItem.LogSubtype = logSubType; logItem.Company = userProfile.Company; logItem.UserName = userProfile.UserName; logItem.Email = userProfile.Email; logItem.Description = description; try { logItem.IPAddress = (request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim().Split(':')[0].ToString(); } catch { logItem.IPAddress = "null"; } LoggingDataService loggingDataService = new LoggingDataService(); return await loggingDataService.LogItemAsync(logItem); }
public async Task<ActionResult> Create(UserProfile model) { //return View(); if (ModelState.IsValid) { string autoGeneratedPassword = Membership.GeneratePassword(8, 2); try { //Create User WebSecurity.CreateUserAndAccount( model.UserName, autoGeneratedPassword, propertyValues: new { Company = model.Company, Email = model.Email }); //Assign to "User" Role Roles.AddUserToRole(model.UserName, "User"); #region Messaging //Extract current domain name: string domainName = Request.UserHostName.ToString(); //Send email: Messaging.MessagingService messaging = new Messaging.MessagingService(); string HtmlBody = "Please login with the information below, and test the logging system for Kaz by clicking on the testing link on the portal page. Thanks! <br/><hr/><br/>username: "******"<br/>" + "password: "******"<br/>" + "<a href='http://" + Request.Url.Host + "/'>LOGIN</a>"; await messaging.SendEmail(model.Email, ProjectSettings.Emails.NewUser_EmailSubjectLine, HtmlBody, ProjectSettings.Emails.NewUser_EmailFrom, ProjectSettings.Emails.NewUser_EmailFromName); #endregion #region Logging UserProfile userProfile = await Sql.SelectStatements.GetUserProfileTask(WebSecurity.CurrentUserName); LoggingDataService loggingDataService = new LoggingDataService(); string description = "New user created [UserName:"******", Email: " + model.Email + ", Company: " + model.Company + "]"; await loggingDataService.LogAsync(userProfile, LogTypes.Platform, PlatformTypes.UserCreated, description, Request); #endregion return RedirectToAction("Index", "Users"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", e.Message); } } return View(model); }