public async Task <IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var info = await Authentication.GetExternalLoginInfoAsync(); if (info == null) { return(InternalServerError()); } var user = new BlogAdmin() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user); if (!result.Succeeded) { return(GetErrorResult(result)); } result = await UserManager.AddLoginAsync(user.Id, info.Login); if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
protected override void Seed(BlogDBContext context) { if (!context.Roles.Any()) { foreach (string iterator in RolesConstants.GetAllRoles()) { context.Roles.Add(new IdentityRole { Name = iterator }); } } if (!context.Users.Any()) { var user = new BlogAdmin { Email = "*****@*****.**", PhoneNumber = "", PhoneNumberConfirmed = false, EmailConfirmed = false, UserName = "******", TwoFactorEnabled = false, LockoutEnabled = false }; _applicationUserManager.CreateAsync(user, "Administrator"); context.Users.AddOrUpdate(user); context.SaveChanges(); var test = _applicationUserManager.AddToRolesAsync(user.Id, RolesConstants.ROLE_SUPER_ADMIN); Console.WriteLine("" + test.Result); context.SaveChanges(); } }
public IActionResult Index() { BlogAdmin model = new BlogAdmin { NumberBlog = _blogService.GetNumberBlog() }; return(View(model)); }
public async Task <IHttpActionResult> CreateAdmin(BlogAdmin inAdmin) { try { return(Ok(await _blogAdminService.CreateAdmin(inAdmin))); } catch (System.SystemException error) { return(BadRequest(error.Message)); } }
///<summary>In PasswordHash field gonna be real password.</summary> ///<exception cref="System.SystemException"></exception> public async Task <BlogAdmin> CreateAdmin(BlogAdmin inAdmin) { await _applicationUserManager.CreateAsync(inAdmin, inAdmin.PasswordHash); IdentityResult res = await _applicationUserManager.AddToRoleAsync(inAdmin.Id, RolesConstants.ROLE_ADMIN); inAdmin = _blogDBContext.Users.Add(inAdmin); await _blogDBContext.SaveChangesAsync(); return(inAdmin); }
public async Task <IHttpActionResult> GetExternalLogin(string provider, string error = null) { if (error != null) { return(Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error))); } if (!User.Identity.IsAuthenticated) { return(new ChallengeResult(provider, this)); } ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity); if (externalLogin == null) { return(InternalServerError()); } if (externalLogin.LoginProvider != provider) { Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); return(new ChallengeResult(provider, this)); } BlogAdmin user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider, externalLogin.ProviderKey)); bool hasRegistered = user != null; if (hasRegistered) { Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager, OAuthDefaults.AuthenticationType); ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName); Authentication.SignIn(properties, oAuthIdentity, cookieIdentity); } else { IEnumerable <Claim> claims = externalLogin.GetClaims(); ClaimsIdentity identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType); Authentication.SignIn(identity); } return(Ok()); }
///<exception cref="System.SystemException"></exception> public async Task UpdateAdmin(BlogAdmin inAdmin) { BlogAdmin old = await _applicationUserManager.FindByIdAsync(inAdmin.Id); old.UserName = inAdmin.UserName; old.Email = inAdmin.Email; old.PhoneNumber = inAdmin.PhoneNumber; old.PasswordHash = inAdmin.PasswordHash; old.Image = inAdmin.Image; old.Contacts = inAdmin.Contacts; await _applicationUserManager.UpdateAsync(old); await _blogDBContext.SaveChangesAsync(); }
protected void fuProfile_PreRender(object sender, EventArgs e) { try { if (fuProfile.HasFile) { if (IsPostBack && fuProfile.PostedFile != null) { string file_name = string.Empty, extension = string.Empty; file_name = fuProfile.FileName; extension = file_name.Substring(file_name.LastIndexOf(".")); if (extension.ToLower().Equals(".png") || extension.ToLower().Equals(".jpg") || extension.ToLower().Equals(".jpeg")) { file_name = file_name.Replace(file_name, hdnUserId.Value + extension); fuProfile.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["profileurl"] + file_name)); imgProfile.ImageUrl = ConfigurationManager.AppSettings["profileurl"] + file_name; db.AddParameter("@userid", Request.QueryString["userid"].ToString()); db.AddParameter("@PicUrl", file_name); db.AddParameter("@blog_id", ConfigurationManager.AppSettings["BlogId"].ToString()); db.ExecuteNonQuery("update usermaster set pic_url=@picurl where userid=@userid and blog_id=@blog_id", CommandType.Text); lblErrorMsg.Text = "Profile Picture Change Successfully."; if (Request.QueryString["type"].ToString() == "user") { BlogAdmin blogmaster = Master as BlogAdmin; blogmaster.userSide(); } else if (Request.QueryString["type"].ToString() == "admin") { AdminMaster admin = Master as AdminMaster; admin.AdminSide(); } } else { lblErrorMsg.Text = "Please select .Png, .Jpg or jpeg file only"; return; } } } } catch (Exception ex) { lblErrorMsg.Text = ex.Message.ToString(); } }
protected void btnUpdate_Click(object sender, EventArgs e) { try { if (btnUpdate.TabIndex == 0) { btnUpdate.Text = "Update"; btnUpdate.TabIndex = 1; txtMobile.ReadOnly = false; txtName.ReadOnly = false; txtLastName.ReadOnly = false; } else { db.AddParameter("@userid", Request.QueryString["userid"].ToString()); db.AddParameter("@mobile", txtMobile.Text); db.AddParameter("@FirstName", txtName.Text); db.AddParameter("@LastName", txtLastName.Text); db.AddParameter("@blog_id", ConfigurationManager.AppSettings["BlogId"].ToString()); db.ExecuteNonQuery("save_user", CommandType.StoredProcedure); if (Request.QueryString["type"].ToString() == "user") { BlogAdmin blogmaster = Master as BlogAdmin; blogmaster.userSide(); } else if (Request.QueryString["type"].ToString() == "admin") { AdminMaster admin = Master as AdminMaster; admin.AdminSide(); } lblErrorMsg.Text = "Profile Updated Successfully."; btnUpdate.Text = "Edit"; btnUpdate.TabIndex = 0; txtMobile.ReadOnly = true; txtName.ReadOnly = true; txtLastName.ReadOnly = true; } } catch (Exception ex) { lblErrorMsg.Text = ex.Message.ToString(); } }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new BlogAdmin() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
///<exception cref="System.SystemException"></exception> public async Task DeleteAdmin(string inAdminId) { BlogAdmin user = await _applicationUserManager.FindByIdAsync(inAdminId); IList <string> rolesForUser = await _applicationUserManager.GetRolesAsync(inAdminId); user.Logins.ToList().ForEach( async login => await _applicationUserManager. RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey))); if (rolesForUser.Count() > 0) { rolesForUser.ToList().ForEach( async role => await _applicationUserManager. RemoveFromRoleAsync(inAdminId, role)); } await _applicationUserManager.DeleteAsync(user); await _blogDBContext.SaveChangesAsync(); }