public IActionResult Create(newUser user) { PasswordHasher <User> hasher = new PasswordHasher <User>(); //check uniquness of username if (_context.users.Where(u => u.email == user.email) .ToList() .Count() > 0) { ModelState.AddModelError("userName", "Username already exists"); } else if (ModelState.IsValid) { User toCreate = new User() { name = user.name, alias = user.alias, email = user.email, password = hasher.HashPassword(user, user.password) }; _context.users.Add(toCreate); _context.SaveChanges(); return(RedirectToAction("Index", "Home")); } return(View("index")); }
private void newUserBtn_Click(object sender, EventArgs e) { newUser form = new newUser(); if (form.ShowDialog() == DialogResult.OK) { user felhasznalo = new user(); felhasznalo.username = form.usernameTextBox.Text; felhasznalo.userpass = AccountManager.getHash(form.usernameTextBox.Text); felhasznalo.adminstatus = form.adminCheckBox.Checked; context.users.Add(felhasznalo); try { context.SaveChanges(); MessageBox.Show("A felhasználó hozzá lett adva az adatbázishoz"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("A felhasználó nem lett hozzáadva az adatbázishoz"); } }
public JsonResult Registration(newUser u) { if (u.password != u.passwordRepeat) { return(Json(new { status = 1 })); } if (string.IsNullOrWhiteSpace(u.name) || string.IsNullOrWhiteSpace(u.password) || string.IsNullOrWhiteSpace(u.passwordRepeat) || string.IsNullOrWhiteSpace(u.email) || string.IsNullOrWhiteSpace(u.surname)) { return(Json(new { status = 1, responseObject = u })); } try { User us = new User { Name = u.name, Surname = u.surname, Email = u.email, Role = 50, Password = Helper.MD5Hash(u.password), ResetPassword = Helper.Random32() }; _db.Users.Add(us); _db.SaveChanges(); return(Json(new { status = 2 })); } catch { return(Json(new { status = 3 })); } }
public App() { InitializeComponent(); userDatabase = new UserDatabaseController(DependencyService.Get <ISQLHelper>().getLocalFilePath("LifeBudgetDB.db3")); // always get instance of the database at the opening of the app //DeleteUsers(); // this is for development purposes // Determine if there is already a user in the data base if (userDatabase.CountUsers() == 0) { MainPage = new newUser(); } else { MainPage = new MasterDetail("Nick"); // change this back to loginPage() } }
public async Task <Users> signUpUsers(string fullname, string CC, string phone, string password, string confirm_password) { newUser user = new newUser() { id = "", fullname = fullname, CC = CC, phone = phone, password = password, confirm_password = confirm_password }; HttpClient cliente = GetConection(); var response = await cliente.PostAsync(url, new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { return(JsonConvert.DeserializeObject <Users>(await response.Content.ReadAsStringAsync())); } return(null); }
//[ValidateAntiForgeryToken] public JsonResult UserRegistration(//String __RequestVerificationToken, String firstname, String lastname, String email, String userPass ) { newUser user_reg = new newUser(); user_reg.msg = "test"; user_reg.status = true; try { userlogin new_user2 = new userlogin(); new_user2.username = firstname + "." + lastname; new_user2.password = Hash(userPass); new_user2.email = email; new_user2.roleID = 1; new_user2.datecreated = DateTime.Now; db.userlogins.Add(new_user2); db.SaveChanges(); user_reg.msg = "User created succesfully!!"; user_reg.status = true; return(Json(user_reg)); } catch (Exception ex) { user_reg.msg = "Error in user creation -- " + ex.Message; user_reg.status = false; return(Json(user_reg)); } }
// checks to see what message we are dealing with. // e.g. "$To" , "$Search" etc private Message DeterminMessage(string StartOfMessage, int firstSpacePosition, Message msg) { Message simpleMessage = new Message(); string[] splitMessage; serverUserInfo userInfo; GHub.client.userInfo usinfo; string nick; switch (StartOfMessage) { // another server is connecting to us and have sent // there username and pass. // //$LogginIn UserName Pass case "$LogginIn": splitMessage = msg.stringFormat.Split(' '); if (splitMessage.Length < 3) { // -1 indicates we are just going to ignore this message simpleMessage.type = -1; return(simpleMessage); } serverLogInInfo logIn = new serverLogInInfo(); logIn.userName = splitMessage[1]; logIn.pass = splitMessage[2]; logIn.type = (int)ServerMessageTypes.LoggingIn; logIn.RawFormat = msg.RawFormat; logIn.From = (int)MessageFrom.Server; logIn.soc = this.soc; logIn.stringFormat = msg.stringFormat; return(logIn); // we are telling this server that it has send a correct // user name and password. case "$loggedIn": simpleMessage.type = (int)ServerMessageTypes.loggedIn; simpleMessage.RawFormat = msg.RawFormat; simpleMessage.From = (int)MessageFrom.Server; simpleMessage.soc = this.soc; simpleMessage.stringFormat = msg.stringFormat; return(simpleMessage); case "$Badpass|": simpleMessage.type = (int)ServerMessageTypes.BadLogIn; simpleMessage.RawFormat = msg.RawFormat; simpleMessage.From = (int)MessageFrom.Server; simpleMessage.soc = this.soc; simpleMessage.stringFormat = msg.stringFormat; return(simpleMessage); // server is closing down case "$Exit": simpleMessage.type = (int)ServerMessageTypes.Exit; simpleMessage.RawFormat = msg.RawFormat; simpleMessage.From = (int)MessageFrom.Server; simpleMessage.soc = this.soc; simpleMessage.stringFormat = msg.stringFormat; return(simpleMessage); // another server is asking us for our username and pass // to be able to log into there server case "$Lock": simpleMessage.type = (int)ServerMessageTypes.LogInInfoPlz; simpleMessage.RawFormat = msg.RawFormat; simpleMessage.From = (int)MessageFrom.Server; simpleMessage.soc = this.soc; simpleMessage.stringFormat = msg.stringFormat; return(simpleMessage); // a new user has logged into another server and they are // telling us about it. the format of the user is as follows. // // $NewUser IsOP=<True/False> $MyINFO $ALL <nick> <interest>$ $<speed>$<e-mail>$<sharesize>$ case "$NewUser": newUser NewUser = new newUser(); userInfo = new serverUserInfo(); nick = userInfo.parseNickFromMyInfo(msg.stringFormat.Substring(msg.stringFormat.IndexOf("$MyINFO"))); if ((usinfo = GHub.client.Client.getnickInfo(nick)) != null) { this.SendMessage("$Exit " + usinfo.nick + "|"); //this.closeAndRemoveUser(); recievedMessages.Clear(); simpleMessage.type = -1; return(simpleMessage); } // add the nick to the list of users held on another server userInfo.rawUserInfo = msg.stringFormat.Substring(msg.stringFormat.IndexOf("$MyINFO")); usersToServer.Add(userInfo); usinfo = new userInfo(msg.soc, this.ServerList, this.ClientList, this.core); usinfo.rawUserInfo = userInfo.rawUserInfo; GHub.client.Client.AddToUserInfoList(usinfo); // add the new usersinfo string to the main array list that holds all the myINFO's // GHub.client.Client.AddToUserInfoList( usinfo ); NewUser.type = (int)ServerMessageTypes.NewUser; NewUser.RawFormat = msg.RawFormat; NewUser.From = (int)MessageFrom.Server; NewUser.soc = this.soc; NewUser.stringFormat = msg.stringFormat; NewUser.slots = userInfo.slots; NewUser.normHubs = userInfo.normHubs; NewUser.regHubs = userInfo.regHubs; NewUser.opHubs = userInfo.opHubs; NewUser.description = userInfo.description; NewUser.email = userInfo.eMail; NewUser.nick = userInfo.nick; return(NewUser); // the server is sending us a users MyINFO that exists // there end. case "$MyINFO": // myINFO string done incorectly so ignore it. if (msg.stringFormat.Length < 9) { // -1 indicates we are just going to ignore this message simpleMessage.type = -1; return(simpleMessage); } userInfo = new serverUserInfo(); nick = userInfo.parseNickFromMyInfo(msg.stringFormat); // if this the the time were 2 servers are connecting to each other // there should be a chance bother servers could have the same nick logged // into there server. We can not let this happen. If this does happen // we will inform the other server we are droping the connection and let them // know why /* * if ( (usinfo = GHub.client.Client.getnickInfo(nick)) != null) * { * this.SendMessage("$Exit " + usinfo.nick + "|"); * //this.closeAndRemoveUser(); * recievedMessages.Clear(); * simpleMessage.type = -1; * return simpleMessage; * }*/ usinfo = GHub.client.Client.getnickInfo(nick); usinfo.rawUserInfo = msg.stringFormat; userInfo = usersToServer.Get(nick); userInfo.rawUserInfo = msg.stringFormat; GHub.client.Client.upDateInfoList(); myInfo info = new myInfo(); info.type = (int)Messagetype.MyINFO; info.RawFormat = msg.RawFormat; info.From = (int)MessageFrom.Server; info.soc = this.soc; info.stringFormat = msg.stringFormat; info.slots = userInfo.slots; info.normHubs = userInfo.normHubs; info.regHubs = userInfo.regHubs; info.opHubs = userInfo.opHubs; info.description = userInfo.description; info.email = userInfo.eMail; info.nick = userInfo.nick; return(info); /* * // the user does not exist in the array list so add it * if (userInfo == null) * { * userInfo = new serverUserInfo(); * userInfo.rawUserInfo = msg.stringFormat; * usersToServer.Add(userInfo); * * info.type =(int)Messagetype.MyINFO; * info.RawFormat = msg.RawFormat; * info.From = (int)MessageFrom.Server; * info.soc = this.soc; * info.stringFormat = msg.stringFormat; * * info.slots = userInfo.slots; * info.normHubs = userInfo.normHubs; * info.regHubs = userInfo.regHubs; * info.opHubs = userInfo.opHubs; * info.description = userInfo.description; * info.email = userInfo.eMail; * info.nick = userInfo.nick; * * usinfo = new userInfo(msg.soc,this.ServerList,this.ClientList); * usinfo.rawUserInfo = msg.stringFormat; * // add the new usersinfo string to the main array list that holds all the myINFO's * GHub.client.Client.AddToUserInfoList( usinfo ); * * return info; * }*/ /* * // update the users info with there new myINFO string. * userInfo.rawUserInfo = msg.stringFormat; * * usinfo = GHub.client.Client.getnickInfo(userInfo.nick); * usinfo.rawUserInfo = userInfo.rawUserInfo; * GHub.client.Client.upDateInfoList(); * * * info.type =(int)Messagetype.MyINFO; * info.RawFormat = msg.RawFormat; * info.From = (int)MessageFrom.Server; * info.soc = this.soc; * info.stringFormat = msg.stringFormat; * * info.slots = userInfo.slots; * info.normHubs = userInfo.normHubs; * info.regHubs = userInfo.regHubs; * info.opHubs = userInfo.opHubs; * info.description = userInfo.description; * info.email = userInfo.eMail; * info.nick = userInfo.nick; * * return info;*/ // this will only be sent during server to server log in. // We will find out about any new user that connects after that // through $NewUser case "$OpList": // OpList string done incorectly so ignore it. if (msg.stringFormat.Length < 9) { // -1 indicates we are just going to ignore this message simpleMessage.type = -1; return(simpleMessage); } simpleMessage.type = (int)ServerMessageTypes.OPList; simpleMessage.RawFormat = msg.RawFormat; simpleMessage.From = (int)MessageFrom.Server; simpleMessage.soc = this.soc; simpleMessage.stringFormat = msg.stringFormat; return(simpleMessage); // a server is asking for our users list // this will happen at the log in process. case "$AllUserInfo": simpleMessage.type = (int)ServerMessageTypes.AllUsersInfo; simpleMessage.RawFormat = msg.RawFormat; simpleMessage.From = (int)MessageFrom.Server; simpleMessage.soc = this.soc; simpleMessage.stringFormat = msg.stringFormat; return(simpleMessage); // a user is leaving on another server case "$Quit": // Quit string done incorectly so ignore it. if (msg.stringFormat.Length < 7) { // -1 indicates we are just going to ignore this message simpleMessage.type = -1; return(simpleMessage); } simpleMessage.type = (int)ServerMessageTypes.UserLeft; simpleMessage.RawFormat = msg.RawFormat; simpleMessage.From = (int)MessageFrom.Server; simpleMessage.soc = this.soc; simpleMessage.stringFormat = msg.stringFormat; return(simpleMessage); // a user on another server is atempting to chat to a user // on our server. case "$To:": splitMessage = msg.stringFormat.Split('$'); string[] FirstPartOfSplit; // not enough parameters in the string for it to be right if (splitMessage.Length < 3) { // -1 indicates we are just going to ignore this message simpleMessage.type = -1; return(simpleMessage); } FirstPartOfSplit = splitMessage[1].Split(' '); // not enough parameters in the string for it to be right if (FirstPartOfSplit.Length < 5) { // -1 indicates we are just going to ignore this message simpleMessage.type = -1; return(simpleMessage); } messageToUser PrivateMessage = new messageToUser(); PrivateMessage.type = (int)Messagetype.ChatPM; PrivateMessage.RawFormat = msg.RawFormat; PrivateMessage.From = (int)MessageFrom.Client; PrivateMessage.soc = this.soc; PrivateMessage.sender = FirstPartOfSplit[3]; PrivateMessage.reciever = FirstPartOfSplit[1]; PrivateMessage.messageToReciever = splitMessage[2]; PrivateMessage.stringFormat = msg.stringFormat; return(PrivateMessage); // a user on another server is searching for somthing // this message must be relayed to all the clients on this // server. case "$Search": splitMessage = msg.stringFormat.Split(' '); if (splitMessage.Length < 3) { // -1 indicates we are just going to ignore this message simpleMessage.type = -1; return(simpleMessage); } messageToUser Search = new messageToUser(); Search.sender = splitMessage[1]; Search.type = (int)Messagetype.Search; Search.RawFormat = msg.RawFormat; Search.From = (int)MessageFrom.Client; Search.soc = this.soc; Search.stringFormat = msg.stringFormat; return(simpleMessage); default: if (StartOfMessage.StartsWith("$")) { // -1 indicates we are just going to ignore this message simpleMessage.type = -1; return(simpleMessage); } mainChat MainChatMessage = new mainChat(); MainChatMessage.type = (int)Messagetype.MainChatMessage; MainChatMessage.RawFormat = msg.RawFormat; MainChatMessage.From = (int)MessageFrom.Client; MainChatMessage.soc = this.soc; MainChatMessage.stringFormat = msg.stringFormat; return(MainChatMessage); } }
public static async Task Initialize(IServiceProvider serviceProvider) { using (var context = new ApplicationDbContext( serviceProvider.GetRequiredService <DbContextOptions <ApplicationDbContext> >())) { var userManager = serviceProvider.GetService <UserManager <ApplicationUser> >(); var roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >(); if (roleManager == null || userManager == null) { throw new Exception("roleManager or userManager is null"); } // Roles var roleNames = new[] { "Teacher", "Student" }; foreach (var name in roleNames) { // Lägg till rollen om den inte finns bool adminRoleExists = await roleManager.RoleExistsAsync(name); if (!adminRoleExists) { var role = new IdentityRole { Name = name }; var result = await roleManager.CreateAsync(role); if (!result.Succeeded) { throw new Exception(string.Join("\n", result.Errors)); } } } // Teacher var newUser = new newUser(); newUser.Name = "Dimitris"; newUser.Email = "*****@*****.**"; newUser.Password = "******"; newUser.Role = "Teacher"; await CreateUserAsync(userManager, roleManager, newUser); // Student newUser = new newUser(); newUser.Name = "Fredrik"; newUser.Email = "*****@*****.**"; newUser.Password = "******"; newUser.Role = "Student"; await CreateUserAsync(userManager, roleManager, newUser); newUser = new newUser(); newUser.Name = "Göran"; newUser.Email = "*****@*****.**"; newUser.Password = "******"; newUser.Role = "Student"; await CreateUserAsync(userManager, roleManager, newUser); newUser = new newUser(); newUser.Name = "Jacob"; newUser.Email = "*****@*****.**"; newUser.Password = "******"; newUser.Role = "Student"; await CreateUserAsync(userManager, roleManager, newUser); newUser = new newUser(); newUser.Name = "Saber"; newUser.Email = "*****@*****.**"; newUser.Password = "******"; newUser.Role = "Student"; await CreateUserAsync(userManager, roleManager, newUser); newUser = new newUser(); newUser.Name = "Melania"; newUser.Email = "*****@*****.**"; newUser.Password = "******"; newUser.Role = "Student"; await CreateUserAsync(userManager, roleManager, newUser); newUser = new newUser(); newUser.Name = "Carola"; newUser.Email = "*****@*****.**"; newUser.Password = "******"; newUser.Role = "Student"; await CreateUserAsync(userManager, roleManager, newUser); var indexPicker = new Random(); // Seed ActivityType if (context.ActivityType.Count() == 0) { var activityTypes = new List <ActivityType>(); activityTypes.Add(new ActivityType { Name = "E-Learning", Description = "Studenten lär sig på egen hand genom att se på video på datorn." }); activityTypes.Add(new ActivityType { Name = "Föreläsning", Description = "Läraren går igenom ett ämne." }); activityTypes.Add(new ActivityType { Name = "Övningstillfälle", Description = "Studenten får göra övningar för att se om hen behärskar ett ämne." }); activityTypes.Add(new ActivityType { Name = "Annat", Description = "Diverse andra aktiviteter." }); //context.ActivityType.RemoveRange(context.ActivityType); context.ActivityType.AddRange(activityTypes); context.SaveChanges(); } // Seed Course if (context.Course.Count() == 0) { var courses = new List <Course>(); courses.Add(new Course { Name = ".NET", Description = "Programmeringskurs i Microsoft .NET.", StartDate = DateTime.Parse("2018-11-26") }); courses.Add(new Course { Name = "Java", Description = "Programmeringskurs i Java.", StartDate = DateTime.Parse("2019-01-05") }); courses.Add(new Course { Name = "Tekniker", Description = "Lär dig bli datatekniker.", StartDate = DateTime.Parse("2019-02-25") }); //context.Course.RemoveRange(context.Course); context.Course.AddRange(courses); context.SaveChanges(); } // Seed ApplicationUserCourse if (context.UserCourse.Count() == 0) { var courseIds = context.Course.Where(c => c.Name == ".NET").Select(a => a.Id).ToArray(); var userIds = context.Users.Select(a => a.Id).ToArray(); var userCourses = new List <ApplicationUserCourse>(); for (int i = 0; i < userIds.Length; i++) { userCourses.Add(new ApplicationUserCourse { ApplicationUserId = userIds[i] , CourseId = courseIds[indexPicker.Next(0, courseIds.Length - 1)] }); } //context.Course.RemoveRange(context.Course); context.UserCourse.AddRange(userCourses); context.SaveChanges(); } // Seed Module .NET Påbyggnadsutbildning if (context.Module.Count() == 0) { var courseIds = context.Course.Where(c => c.Name == ".NET").Select(a => a.Id).ToArray(); var modules = new List <Module>(); modules.Add(new Module { Name = "C#", CourseId = courseIds[indexPicker.Next(0, courseIds.Length - 1)], Description = "C# 7 (Objektorienterad programmering, Generics, Linq)", StartTime = DateTime.Parse("2018-11-26"), EndTime = DateTime.Parse("2018-12-14") }); modules.Add(new Module { Name = "Testning", CourseId = courseIds[indexPicker.Next(0, courseIds.Length - 1)], Description = "Unittestning mm.", StartTime = DateTime.Parse("2018-12-17"), EndTime = DateTime.Parse("2019-01-02") }); modules.Add(new Module { Name = "Webb", CourseId = courseIds[indexPicker.Next(0, courseIds.Length - 1)], Description = "HTML5, CSS3, JavaScript.", StartTime = DateTime.Parse("2019-01-03"), EndTime = DateTime.Parse("2019-01-14") }); modules.Add(new Module { Name = "MVC", CourseId = courseIds[indexPicker.Next(0, courseIds.Length - 1)], Description = "ASP.NET MVC Core.", StartTime = DateTime.Parse("2019-01-15"), EndTime = DateTime.Parse("2019-01-30") }); modules.Add(new Module { Name = "Databaser", CourseId = courseIds[indexPicker.Next(0, courseIds.Length - 1)], Description = "Entity framework & SQL.", StartTime = DateTime.Parse("2019-01-31"), EndTime = DateTime.Parse("2019-02-08") }); modules.Add(new Module { Name = "Webbapplikationer", CourseId = courseIds[indexPicker.Next(0, courseIds.Length - 1)], Description = "Förhöj användarupplevelsen.", StartTime = DateTime.Parse("2019-02-11"), EndTime = DateTime.Parse("2019-02-21") }); modules.Add(new Module { Name = "MVC Fördjupning", CourseId = courseIds[indexPicker.Next(0, courseIds.Length - 1)], Description = "Projektarbete Full-stack applikation.", StartTime = DateTime.Parse("2019-02-22"), EndTime = DateTime.Parse("2019-03-22") }); //context.Module.RemoveRange(context.Module); context.Module.AddRange(modules); context.SaveChanges(); } // Seed ActivityModel if (context.ActivityModel.Count() == 0) { string[] randomNames = { "Scott Allen", "Susan Johnson", "Bill Gates", "Jeff Bezos", "Steve Balmer" }; string[] randomWords = { "Fördjupning", "Grunderna", "Info om", "Mera om" }; var activityTypes = context.ActivityType.ToArray(); var modules = context.Module.ToArray(); var activityModels = new List <ActivityModel>(); foreach (var module in modules) { for (int i = 0; i < indexPicker.Next(1, 10); i++) { var activityTypeIndex = indexPicker.Next(0, activityTypes.Length - 1); var dateIndex = (module.EndTime - module.StartTime).Days; var startDate = module.StartTime.AddDays(indexPicker.Next(0, dateIndex - 3)).AddHours(9); var stopDate = startDate.AddDays(indexPicker.Next(0, 3)).AddHours(8); activityModels.Add(new ActivityModel { Name = randomWords[indexPicker.Next(0, randomWords.Length - 1)] + " " + module.Name, ModuleId = module.Id, ActivityTypeId = activityTypes[activityTypeIndex].Id, Description = activityTypes[activityTypeIndex].Name + " med " + randomNames[indexPicker.Next(0, randomNames.Length - 1)] + ".", StartDate = startDate, StopDate = stopDate }); } } context.ActivityModel.AddRange(activityModels); context.SaveChanges(); } } }
private static async Task <bool> CreateUserAsync(UserManager <ApplicationUser> userManager, RoleManager <IdentityRole> roleManager, newUser newUser) { Boolean success = false; if (userManager != null && roleManager != null) { var foundUser = await userManager.FindByEmailAsync(newUser.Email); if (foundUser == null) { var user = new ApplicationUser { Name = newUser.Name, UserName = newUser.Email, Email = newUser.Email }; var addUserResult = await userManager.CreateAsync(user, newUser.Password); if (addUserResult.Succeeded) { var addToRoleResultAdmin = await userManager.AddToRoleAsync(user, newUser.Role); if (addToRoleResultAdmin.Succeeded) { success = true; } } } } return(success); }