protected void CreateUser_Click(object sender, EventArgs e) { string firstName = FirstName.Text; string lastName = LastName.Text; string userName = UserName.Text; string email = Email.Text; var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); ApplicationUser u = new ApplicationUser() { FirstName = firstName, LastName = lastName, UserName = userName, Email = email }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void LogIn(object sender, EventArgs e) { if (IsValid) { var context = new ApplicationDbContext(); var signinUser = context.Users.FirstOrDefault(u => u.UserName == UserName.Text); if (signinUser!= null && !signinUser.IsDeleted) { // Validate the user password IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication; IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { FailureText.Text = result.Errors.FirstOrDefault(); ErrorMessage.Visible = true; } } else { FailureText.Text = "Please register"; ErrorMessage.Visible = true; } } }
protected void Page_Load(object sender, EventArgs e) { //var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); //manager.Roles.CreateRoleAsync(new Role("Moderator")); //manager.Roles.AddUserToRoleAsync("54fbcab5-d65b-47d4-b517-2eba10002e21", "186c2138-de27-42a6-94f0-8b830c2e8b9c"); var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); if (!manager.Logins.HasLocalLogin(User.Identity.GetUserId())) { grdMessages.EmptyDataTemplate = null; if (grdMessages.FooterRow != null) { grdMessages.FooterRow.Visible = false; } } else { var userId = User.Identity.GetUserId(); var db = new ApplicationDbContext(); var user = db.Users.FirstOrDefault(u => u.Id == userId); if (user != null) { if (user.Roles.Any(r => r.Role.Name == "Moderator")) { grdMessages.Columns[2].Visible = true; } else if (user.Roles.Any(r => r.Role.Name == "Administrator")) { grdMessages.Columns[2].Visible = true; grdMessages.Columns[3].Visible = true; } } } }
protected void CreateUser_Click(object sender, EventArgs e) { TwitterEntities context = new TwitterEntities(); string userName = UserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore()); User u = new User(userName) { UserName = userName }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); var role = context.AspNetRoles.FirstOrDefault(x => x.Name == "User"); if (result.Success) { string userId = u.Id; AspNetUser currentUser = context.AspNetUsers.FirstOrDefault(x => x.Id == userId); currentUser.AspNetRoles.Add(role); context.SaveChanges(); manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore()); User u = new User(userName) { UserName = userName }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); //OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); using (SocialNetworkDbEntities context = new SocialNetworkDbEntities()) { UserDetail details = new UserDetail(); context.UserDetails.Add(details); context.SaveChanges(); var user = context.AspNetUsers.Find(u.Id); user.UserDetailsId = details.UserDetailsId; context.SaveChanges(); } Response.Redirect("~/Account/UserDetails"); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void LogIn(object sender, EventArgs e) { if (IsValid) { // Validate the user password IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication; IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { var errorMessageText = result.Errors.FirstOrDefault(); FailureText.Text = errorMessageText; ErrorMessage.Visible = true; ErrorSuccessNotifier.AddErrorMessage(errorMessageText); } } else { ErrorSuccessNotifier.AddErrorMessage( "An unexpected error occured! Please refresh the page..."); } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; string firstName = FirstName.Text; string lastName = LastName.Text; string email = Email.Text; var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); ApplicationUser u = new ApplicationUser() { UserName = userName, FirstName = firstName, LastName = lastName, Email = email, DisplayName = firstName + " " + lastName }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void LogIn(object sender, EventArgs e) { if (IsValid) { // Validate the user password IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication; IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked); if (result.Success) { var context = new ApplicationDbContext(); var curUserName = UserName.Text; var userId = context.Users.FirstOrDefault(u => u.UserName == curUserName).Id; var userRole = context.UserRoles.FirstOrDefault(u => u.UserId == userId).Role.Name; if (userRole == "Banned") { ErrorSuccessNotifier.AddErrorMessage("You are banned!"); return; } OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault()); } } }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var manager = new AuthenticationIdentityManager(new IdentityStore(new TvvitterContext())); // Create a local login before signing in the user var user = new ApplicationUser() { UserName = model.UserName, }; var result = manager.Users.CreateLocalUser(user, model.Password); //var result = await IdentityManager.Users.CreateLocalUserAsync(user, model.Password); if (result.Success) { await IdentityManager.Authentication.SignInAsync(AuthenticationManager, user.Id, isPersistent : false); return(RedirectToAction("Index", "Home")); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return(View(model)); }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; var db = new ApplicationDbContext(); var manager = new AuthenticationIdentityManager(new IdentityStore(db)); ApplicationUser u = new ApplicationUser() { UserName = userName, FirstName = tb_FirstName.Text, LastName = tb_LastName.Text, Email = tb_Email.Text }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); var foundRole = db.Roles.Single(x => x.Name == dl_Roles.SelectedValue); u.Roles = new System.Collections.Generic.HashSet<UserRole>(); u.Roles.Add(new UserRole() { Role = foundRole, User = u }); db.SaveChanges(); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void LinkButtonBanUser_Command(object sender, CommandEventArgs e) { var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); string roleBanId = "2"; manager.Roles.AddUserToRoleAsync(e.CommandArgument.ToString(), roleBanId); }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore(new AcademyDbContext())); ApplicationUser u = new ApplicationUser(userName) { UserName = userName, FirstName = this.TextBoxFirstName.Text, LastName = this.TextBoxLastName.Text, Email = this.TextBoxEmail.Text, JoinDate = DateTime.Now, }; var context = new AcademyDbContext(); string fileName = string.Empty; var fileUpload = this.FileUploadAvatar; if (fileUpload.HasFile) { if (fileUpload.PostedFile.ContentLength < 102400 && (fileUpload.PostedFile.ContentType == PngImageFormat || fileUpload.PostedFile.ContentType == JpegImageFormat || fileUpload.PostedFile.ContentType == GifImageFormat)) { fileName = userName.Replace("<", string.Empty).Replace(">", string.Empty) + GetAvatarExtension(FileUploadAvatar.PostedFile.FileName); fileUpload.SaveAs(Server.MapPath(MainPath) + fileName); u.AvatarPath = MainPath + fileName; } else { ErrorSuccessNotifier.ShowAfterRedirect = true; ErrorSuccessNotifier.AddErrorMessage("The uploaded avatar exceeds 100KB or is in a wrong format."); Response.Redirect(Request.RawUrl, false); return; } } else { u.AvatarPath = DefaultImagePath; } IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); ErrorSuccessNotifier.ShowAfterRedirect = true; ErrorSuccessNotifier.AddSuccessMessage("Registration completed successfully."); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault()); } }
public AccountController() { var context = new MoviesContext(); userRole = context.Roles .Where(r => r.Name == "user") .Select(r => r.Id) .FirstOrDefault() .ToString(); IdentityManager = new AuthenticationIdentityManager(new IdentityStore(context)); }
protected void CreateUser_Click(object sender, EventArgs e) { bool error = false; string userName = UserName.Text; if (userName == "" || userName.Length < 3) { ErrorSuccessNotifier.AddErrorMessage("The username must be at least 3 characters"); error = true; } string password = Password.Text; if (password == "" || password.Length < 6) { ErrorSuccessNotifier.AddErrorMessage("The password must be at least 6 characters"); error = true; } string confirmPassword = ConfirmPassword.Text; if (confirmPassword != password) { ErrorSuccessNotifier.AddErrorMessage("The passwords does not match"); error = true; } if (error) { return; } var manager = new AuthenticationIdentityManager(new IdentityStore()); User u = new User(userName) { UserName = userName }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { ErrorSuccessNotifier.AddSuccessMessage("Successfully registered"); manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void Page_Load() { // Process the result from an auth provider in the request ProviderName = OpenAuthProviders.GetProviderNameFromRequest(Request); if (String.IsNullOrEmpty(ProviderName)) { Response.Redirect("~/Account/Login"); } if (!IsPostBack) { IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication; var auth = Context.GetOwinContext().Authentication; ClaimsIdentity id = manager.GetExternalIdentity(auth); var context = new ApplicationDbContext(); var user = context.UserRoles.FirstOrDefault(u => u.User.UserName == id.Name); if (user != null) { var userRole = user.Role.Name; if (userRole == "Banned") { ErrorSuccessNotifier.AddErrorMessage("You are banned!"); ErrorSuccessNotifier.ShowAfterRedirect = true; Response.Redirect("~/Account/Login"); } } IdentityResult result = manager.SignInExternalIdentity(auth, id); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else if (User.Identity.IsAuthenticated) { result = manager.LinkExternalIdentity(id, User.Identity.GetUserId()); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { AddErrors(result); return; } } else { userName.Text = id.Name; } } }
private async void Redirect() { AuthenticationIdentityManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); var userId = User.Identity.GetUserId(); var roles = await manager.Roles.GetRolesForUserAsync(userId); if (roles.Any(r => r.Name == "Administrator")) { Response.Redirect("~/Administrator/AdministratorDefault.aspx"); } else if (roles.Any(r => r.Name == "Moderator")) { Response.Redirect("~/Moderator/ModeratorDefault.aspx"); } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; string email = Email.Text; string filename = "default.png"; if (UploadAvatar.HasFile) { if (UploadAvatar.PostedFile.ContentType == "image/jpeg" || UploadAvatar.PostedFile.ContentType == "image/gif" || UploadAvatar.PostedFile.ContentType == "image/png") { if (UploadAvatar.PostedFile.ContentLength < 102400) { filename = userName + Path.GetExtension(UploadAvatar.FileName); UploadAvatar.SaveAs(Server.MapPath("~/Avatar_Files/") + filename); } else { ErrorSuccessNotifier.AddErrorMessage("Upload status: The file has to be less than 100 kb!"); return; } } else { ErrorSuccessNotifier.AddErrorMessage("Upload status: Only JPEG files are accepted!"); return; } } var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); ApplicationUser u = new ApplicationUser() { UserName = userName, Email = email, Avatar = filename }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault()); } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore()); User u = new User(userName) { UserName = userName }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl("~/library-system/Default.aspx", Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = this.UserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore(new BooksLibrarySystemContext())); User u = new User(userName) { UserName = userName }; IdentityResult result = manager.Users.CreateLocalUser(u, this.Password.Text); if (result.Success) { manager.Authentication.SignIn(this.Context.GetOwinContext().Authentication, u.Id, isPersistent: false); this.Response.Redirect(this.Request.QueryString["ReturnUrl"], false); //BooksLibrarySystem.Web.Account.OpenAuthProviders.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response); } else { this.ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void LogIn(object sender, EventArgs e) { if (IsValid) { // Validate the user password IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication; IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault()); } } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = TextBoxUserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); User u = new User(userName); IdentityResult result = manager.Users.CreateLocalUser(u, TextBoxPassword.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void LogIn(object sender, EventArgs e) { if (IsValid) { // Validate the user password IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication; IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl("~/library-system/Default.aspx", Response); } else { FailureText.Text = result.Errors.FirstOrDefault(); ErrorMessage.Visible = true; } } }
protected void LogIn(object sender, EventArgs e) { if (this.IsValid) { // Validate the user password IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new BooksLibrarySystemContext())).Authentication; IdentityResult result = manager.CheckPasswordAndSignIn(this.Context.GetOwinContext().Authentication, this.UserName.Text, this.Password.Text, this.RememberMe.Checked); if (result.Success) { this.Response.Redirect(this.Request.QueryString["ReturnUrl"], false); //OpenAuthProviders.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response); } else { this.FailureText.Text = result.Errors.FirstOrDefault(); this.ErrorMessage.Visible = true; } } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore(new ForumEmeraldContext())); string filename = string.Empty; ForumUser user = new ForumUser() { UserName = userName, Email = this.TextBoxEmail.Text, }; var fileUpload = this.FileUploadPhoto; if (fileUpload.HasFile && (fileUpload.PostedFile.ContentType == PngImageFormat || FileUploadPhoto.PostedFile.ContentType == JpegImageFormat)) { filename = userName + GetPhotoExtension(FileUploadPhoto.PostedFile.FileName); fileUpload.SaveAs(Server.MapPath(MainPath) + filename); user.PhotoPath = MainPath + filename; } else { user.PhotoPath = DefaultImagePath; } IdentityResult result = manager.Users.CreateLocalUser(user, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, user.Id, isPersistent: false); this.AddUserToRole(); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { if (this.FileUploadPhoto.HasFile) { File.Delete(this.Server.MapPath(MainPath + filename)); } ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
private void CreateAndLoginUser() { if (!IsValid) { return; } var user = new User(userName.Text); IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication; IdentityResult result = manager.CreateAndSignInExternalUser(Context.GetOwinContext().Authentication, user); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { AddErrors(result); return; } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); User u = new User(userName) { UserName = userName }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); ErrorSuccessNotifier.AddSuccessMessage("Welcome back."); ErrorSuccessNotifier.ShowAfterRedirect = true; OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorSuccessNotifier.AddErrorMessage("Register details are incorect."); ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
private void CreateAndLoginUser() { if (!this.IsValid) { return; } var user = new User(this.userName.Text); IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new BooksLibrarySystemContext())).Authentication; IdentityResult result = manager.CreateAndSignInExternalUser(this.Context.GetOwinContext().Authentication, user); if (result.Success) { BooksLibrarySystem.Web.Account.OpenAuthProviders.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response); } else { this.AddErrors(result); return; } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; string fullName = this.FullName.Text; string email = this.Email.Text; string summary = this.Summary.Text; string localPath = string.Empty; string relativePath = string.Empty; if (FileUploadControl.HasFile) { string filename = Path.GetFileName(FileUploadControl.FileName); localPath = Server.MapPath("~/Uploaded_Files/") + filename; relativePath = "/Uploaded_Files/" + filename; FileUploadControl.SaveAs(localPath); StatusLabel.Text = "Upload status: File uploaded!"; } var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); var context = new ApplicationDbContext(); ApplicationUser u = new ApplicationUser() { UserName = userName, FullName = fullName, Email = email, Summary = summary, AvatarUrl = relativePath, }; var adminId = context.Roles.FirstOrDefault(r => r.Name == "User").Id.ToString(); manager.Roles.AddUserToRoleAsync(u.Id, adminId); IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
private async void CreateUser(string userName, string password, string firstName, string lastName, string email) { var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); var role = await manager.Roles.FindRoleByNameAsync("Registered user"); if (role == null) { ErrorMessage.Text = "Role \"Registered user\" missing."; return; } ApplicationUser user = new ApplicationUser() { UserName = userName, FirstName = firstName, LastName = lastName, Email = email }; IdentityResult result = manager.Users.CreateLocalUser(user, password); if (result.Success) { IdentityResult addToRoleResult = await manager.Roles.AddUserToRoleAsync(user.Id, role.Id); if (addToRoleResult.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, user.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = addToRoleResult.Errors.FirstOrDefault(); } } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); ApplicationUser user = new ApplicationUser() { UserName = userName, AvatarLink = DEFAULT_AVATAR_NAME }; IdentityResult result = manager.Users.CreateLocalUser(user, Password.Text); if (result.Success) { CreateDefaultRoleForRegisteringUser(user); manager.Authentication.SignIn(Context.GetOwinContext().Authentication, user.Id, isPersistent: false); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void LogIn(object sender, EventArgs e) { if (IsValid) { bool error = false; string username = this.UserName.Text; if (username == "") { ErrorSuccessNotifier.AddErrorMessage("The username field is required"); error = true; } string password = this.Password.Text; if (password == "") { ErrorSuccessNotifier.AddErrorMessage("The password field is required"); error = true; } if (error) { return; } // Validate the user password IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication; IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { FailureText.Text = result.Errors.FirstOrDefault(); ErrorMessage.Visible = true; } } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); ApplicationUser u = new ApplicationUser(userName) { UserName = userName, ProfilePicture = CreateDefaultImg()}; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); //manager.Roles.FindRoleByNameAsync("Registered") // .ContinueWith(role => // { //manager.Roles.AddUserToRoleAsync(u.Id, role.Id.ToString()); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); //}); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void ButtonBAN_Click(object sender, EventArgs e) { var context = new ApplicationDbContext(); var user = context.Users.Find(userId); var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); if (user.Roles.Count == 0 || user.Roles.First().Role.Name == "Admin") { var roleId = "2"; manager.Roles.AddUserToRoleAsync(userId, roleId); ErrorSuccessNotifier.AddInfoMessage("The User has been banned!"); this.ButtonBAN.Text = "Unban User"; this.ButtonBAN.OnClientClick = "return confirm('Do you want to Unban user ?');"; } else { var roleId = user.Roles.First().RoleId; manager.Roles.RemoveUserFromRoleAsync(userId, roleId); ErrorSuccessNotifier.AddInfoMessage("The User has been unbanned!"); this.ButtonBAN.Text = "BAN User"; this.ButtonBAN.OnClientClick = "return confirm('Do you want to Ban user ?');"; } }
protected void ButtonBAN_Click(object sender, EventArgs e) { var context = new ApplicationDbContext(); var user = context.Users.Find(userId); var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); if (user.Roles.Count == 0 || user.Roles.First().Role.Name == "Admin") { var roleId = "2"; manager.Roles.AddUserToRoleAsync(userId, roleId); ErrorSuccessNotifier.AddInfoMessage("The User has been banned!"); this.ButtonBAN.Text = "Unban User"; this.ButtonBAN.OnClientClick = "return confirm('Do you want to Unban user ?');"; } else { var roleId = user.Roles.First().RoleId; manager.Roles.RemoveUserFromRoleAsync(userId, roleId); ErrorSuccessNotifier.AddInfoMessage("The User has been unbanned!"); this.ButtonBAN.Text = "Ban User"; this.ButtonBAN.OnClientClick = "return confirm('Do you want to Ban user ?');"; } }
protected void LogIn(object sender, EventArgs e) { if (IsValid) { // Validate the user password IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication; IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked); if (result.Success) { ErrorSuccessNotifier.AddSuccessMessage("Welcome back."); ErrorSuccessNotifier.ShowAfterRedirect = true; OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { //FailureText.Text = result.Errors.FirstOrDefault(); //ErrorMessage.Visible = true; ErrorSuccessNotifier.AddErrorMessage("Login details are incorect."); //ErrorSuccessNotifier.ShowAfterRedirect = true; } } }
protected void Page_Load() { // Process the result from an auth provider in the request ProviderName = OpenAuthProviders.GetProviderNameFromRequest(Request); if (String.IsNullOrEmpty(ProviderName)) { Response.Redirect("~/Account/Login"); } if (!IsPostBack) { IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication; var auth = Context.GetOwinContext().Authentication; ClaimsIdentity id = manager.GetExternalIdentity(auth); IdentityResult result = manager.SignInExternalIdentity(auth, id); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else if (User.Identity.IsAuthenticated) { result = manager.LinkExternalIdentity(id, User.Identity.GetUserId()); if (result.Success) { OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { AddErrors(result); return; } } else { userName.Text = id.Name; } } }
protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; var db = new ApplicationDbContext(); var manager = new AuthenticationIdentityManager(new IdentityStore(db)); ApplicationUser u = new ApplicationUser() { UserName = userName, FirstName = tb_FirstName.Text, LastName = tb_LastName.Text, Email = tb_Email.Text }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); var foundRole = db.Roles.Single(x => x.Name == dl_Roles.SelectedValue); u.Roles = new System.Collections.Generic.HashSet <UserRole>(); u.Roles.Add(new UserRole() { Role = foundRole, User = u }); db.SaveChanges(); OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void ButtonAdmin_Click(object sender, EventArgs e) { var context = new ApplicationDbContext(); var user = context.Users.Find(userId); var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())); if (user.Roles.Count == 0 || user.Roles.First().Role.Name == "Banned") { var roleId = "1"; manager.Roles.AddUserToRoleAsync(userId, roleId); ErrorSuccessNotifier.AddInfoMessage("The User is an administrator!"); this.ButtonAdmin.Text = "Remove Admin"; this.ButtonAdmin.OnClientClick = "return confirm('Do you want to remove admin?');"; } else { var roleId = user.Roles.First().RoleId; manager.Roles.RemoveUserFromRoleAsync(userId, roleId); ErrorSuccessNotifier.AddInfoMessage("The User is not an administrator!"); this.ButtonAdmin.Text = "Add Admin"; this.ButtonAdmin.OnClientClick = "return confirm('Do you want to create admin?');"; } }
public AccountController(AuthenticationIdentityManager manager) { IdentityManager = manager; }
public AccountController() { IdentityManager = new AuthenticationIdentityManager(new IdentityStore(new TwitterDbContext())); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { // Create a local login before signing in the user var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; var manager = new AuthenticationIdentityManager(new IdentityStore(new LaptopsDbContext())); //var result = await IdentityManager.Users.CreateLocalUserAsync(user, model.Password); var result = manager.Users.CreateLocalUser(user, model.Password); if (result.Success) { await IdentityManager.Authentication.SignInAsync(AuthenticationManager, user.Id, isPersistent: false); return RedirectToAction("Index", "Home"); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); }
public AccountController() { IdentityManager = new AuthenticationIdentityManager(new IdentityStore()); }
public AccountController(ILaptopListingSystemData data, AuthenticationIdentityManager manager) : this(data) { this.IdentityManager = manager; }
public UserAdministrationController() { IdentityManager = new AuthenticationIdentityManager(new IdentityStore(new ForumDbContext())); }