public SimpleMembershipInitializer() { Database.SetInitializer <CourseAiderContext>(null); try { using (var context = new CourseAiderContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); if (!Roles.RoleExists("Teacher")) { Roles.CreateRole("Teacher"); } if (!Roles.RoleExists("Student")) { Roles.CreateRole("Student"); } } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } }
public override System.Threading.Tasks.Task OnConnected() { string name = this.Context.User.Identity.Name; var ircClient = new IrcClient(); UserProfile profile; using (CourseAiderContext context = new CourseAiderContext()) { profile = context.UserProfiles.FirstOrDefault(prof => prof.UserName == this.Context.User.Identity.Name); Clients.Caller.notify("Transfering credentials..."); } var chatContext = new ChatContext() { ChatClient = ircClient, IsTeacher = profile.IsTeacher, UserId = profile.UserId, UserName = profile.UserName }; contexts.Add(name, chatContext); string message = "Hello ," + name; Clients.Caller.notify(message); return(base.OnConnected()); }
public ActionResult UserFiles() { using (CourseAiderContext context = new CourseAiderContext()) { var files = context.Files.Where(c => c.Uploader.UserName == WebSecurity.CurrentUserName); return(PartialView("_UserFiles", files.ToList())); } }
public ActionResult UserGroups() { using (CourseAiderContext context = new CourseAiderContext()) { var groups = context.Groups.Where(c => c.Members.Any(a => a.UserName == WebSecurity.CurrentUserName)); return(PartialView("_UserGroups", groups.ToList())); } }
// // GET: /Course/ public ActionResult Index() { bool isTeacher = false; using (CourseAiderContext context = new CourseAiderContext()) { var profile = context.UserProfiles.FirstOrDefault(p => p.UserName == WebSecurity.CurrentUserName); if (profile != null) { isTeacher = profile.IsTeacher; } } ViewBag.isTeacher = isTeacher; return(View(db.Courses.ToList())); }
public string Post(int id) { using (var a = new CourseAiderContext()) { var file = a.Files.Find(id); if (file != null) { if (file.Score == 0) { file.Score += 1; file.Uploader.Score += 1; a.SaveChanges(); } } } return(""); }
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return(RedirectToAction("Manage")); } if (ModelState.IsValid) { // Insert a new user into the database using (CourseAiderContext db = new CourseAiderContext()) { UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return(RedirectToLocal(returnUrl)); } else { ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public void AnswerQuestion(string questionId, int answer) { if (!this.Context.User.Identity.IsAuthenticated) { return; } questions = questions.Where(q => q.Value.ExpirationTime > DateTime.Now).ToDictionary(a => a.Key, a => a.Value); if (questions.ContainsKey(questionId)) { var question = questions[questionId]; if (question.Answerers.Contains(this.Context.User.Identity.Name)) { return; } question.Answerers.Add(this.Context.User.Identity.Name); if (answer == question.CorrectAnswer) { using (var db = new CourseAiderContext()) { var user = db.UserProfiles.FirstOrDefault(a => a.UserName == this.Context.User.Identity.Name); if (user == null) { return; } user.Score += question.Points; db.SaveChanges(); } Clients.Caller.notify("You have answered correctly"); } else { Clients.Caller.notify("You have answered incorrectly , the correct answer was '" + question.Answers[question.CorrectAnswer] + "'."); } } }
public void Connect(string channel) { if (!this.Context.User.Identity.IsAuthenticated) { return; } string password; UserProfile prof; using (CourseAiderContext context = new CourseAiderContext()) { prof = context.UserProfiles.FirstOrDefault(profile => profile.UserName == this.Context.User.Identity.Name); password = prof.IrcCredential; Clients.Caller.notify("Transfering credentials..."); } ChatContext ircContext; if (contexts.TryGetValue(this.Context.User.Identity.Name, out ircContext)) { IrcClient ircClient = ircContext.ChatClient; Action closure = () => { string user = this.Context.User.Identity.Name; string pass = password; string chan = channel; var caller = Clients.Caller; IrcClient client = ircClient; ircClient.Registered += (object sender, EventArgs e) => { Clients.Caller.notify("Joining chat channel..."); client.Channels.Join("#" + chan); RegisterClientEvents(client); }; ircClient.Error += (object sender, IrcErrorEventArgs e) => { Clients.Caller.error("Web Server:" + e.Error.Message + e.Error.StackTrace); }; ircClient.ErrorMessageReceived += (object sender, IrcErrorMessageEventArgs e) => { Clients.Caller.error("IRC Server:" + e.Message); }; ircClient.ProtocolError += (object sender, IrcProtocolErrorEventArgs e) => { string error = e.Message; if (e.Parameters != null && e.Parameters.Count > 0) { error += ", params:" + e.Parameters.Aggregate((a, b) => a + "," + b); } Clients.Caller.error("Protocol:" + error); }; }; closure(); Clients.Caller.notify("Attempting connection..."); ircClient.Connect(new Uri(Configuration.IrcServerUri), new IrcUserRegistrationInfo() { NickName = prof.UserName, UserName = prof.UserName, RealName = prof.RealName, Password = password, }); } }