Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ab55469

Browse files
Change password in asp.net core
1 parent 9b9eb05 commit ab55469

File tree

7 files changed

+110
-3
lines changed

7 files changed

+110
-3
lines changed

‎Webgentle.BookStore/Webgentle.BookStore/Controllers/AccountController.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,33 @@ public async Task<IActionResult> Logout()
8383
await _accountRepository.SignOutAsync();
8484
return RedirectToAction("Index", "Home");
8585
}
86+
87+
[Route("change-password")]
88+
public IActionResult ChangePassword()
89+
{
90+
return View();
91+
}
92+
93+
[HttpPost("change-password")]
94+
public async Task<IActionResult> ChangePassword(ChangePasswordModel model)
95+
{
96+
if (ModelState.IsValid)
97+
{
98+
var result = await _accountRepository.ChangePasswordAsync(model);
99+
if (result.Succeeded)
100+
{
101+
ViewBag.IsSuccess = true;
102+
ModelState.Clear();
103+
return View();
104+
}
105+
106+
foreach (var error in result.Errors)
107+
{
108+
ModelState.AddModelError("", error.Description);
109+
}
110+
111+
}
112+
return View(model);
113+
}
86114
}
87115
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace Webgentle.BookStore.Models
8+
{
9+
public class ChangePasswordModel
10+
{
11+
[Required, DataType(DataType.Password), Display(Name ="Current password")]
12+
public string CurrentPassword { get; set; }
13+
[Required, DataType(DataType.Password), Display(Name = "New password")]
14+
public string NewPassword { get; set; }
15+
[Required, DataType(DataType.Password), Display(Name = "Confirm new password")]
16+
[Compare("NewPassword", ErrorMessage = "Confirm new password does not match")]
17+
public string ConfirmNewPassword { get; set; }
18+
}
19+
}

‎Webgentle.BookStore/Webgentle.BookStore/Repository/AccountRepository.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44
using System.Linq;
55
using System.Threading.Tasks;
66
using Webgentle.BookStore.Models;
7+
using Webgentle.BookStore.Service;
78

89
namespace Webgentle.BookStore.Repository
910
{
1011
public class AccountRepository : IAccountRepository
1112
{
1213
private readonly UserManager<ApplicationUser> _userManager;
1314
private readonly SignInManager<ApplicationUser> _signInManager;
15+
private readonly IUserService _userService;
1416

15-
public AccountRepository(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
17+
public AccountRepository(UserManager<ApplicationUser> userManager,
18+
SignInManager<ApplicationUser> signInManager,
19+
IUserService userService)
1620
{
1721
_userManager = userManager;
1822
_signInManager = signInManager;
23+
_userService = userService;
1924
}
2025

2126
public async Task<IdentityResult> CreateUserAsync(SignUpUserModel userModel)
@@ -41,5 +46,12 @@ public async Task SignOutAsync()
4146
{
4247
await _signInManager.SignOutAsync();
4348
}
49+
50+
public async Task<IdentityResult> ChangePasswordAsync(ChangePasswordModel model)
51+
{
52+
var userId = _userService.GetUserId();
53+
var user = await _userManager.FindByIdAsync(userId);
54+
return await _userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);
55+
}
4456
}
4557
}

‎Webgentle.BookStore/Webgentle.BookStore/Repository/IAccountRepository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ public interface IAccountRepository
1111
Task<SignInResult> PasswordSignInAsync(SignInModel signInModel);
1212

1313
Task SignOutAsync();
14+
15+
Task<IdentityResult> ChangePasswordAsync(ChangePasswordModel model);
1416
}
1517
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@model Webgentle.BookStore.Models.ChangePasswordModel
2+
3+
@{
4+
ViewData["Title"] = "ChangePassword";
5+
}
6+
<div class="container">
7+
8+
<h1 class="display-4 text-center">Change password</h1>
9+
10+
<hr />
11+
<div class="row">
12+
13+
<div class="col-md-4 offset-4">
14+
@if (ViewBag.IsSuccess == true)
15+
{
16+
<div class="alert alert-success" role="alert">
17+
Password has been updated successfully
18+
</div>
19+
}
20+
<form asp-action="ChangePassword">
21+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
22+
<div class="form-group">
23+
<label asp-for="CurrentPassword" class="control-label"></label>
24+
<input asp-for="CurrentPassword" class="form-control" />
25+
<span asp-validation-for="CurrentPassword" class="text-danger"></span>
26+
</div>
27+
<div class="form-group">
28+
<label asp-for="NewPassword" class="control-label"></label>
29+
<input asp-for="NewPassword" class="form-control" />
30+
<span asp-validation-for="NewPassword" class="text-danger"></span>
31+
</div>
32+
<div class="form-group">
33+
<label asp-for="ConfirmNewPassword" class="control-label"></label>
34+
<input asp-for="ConfirmNewPassword" class="form-control" />
35+
<span asp-validation-for="ConfirmNewPassword" class="text-danger"></span>
36+
</div>
37+
<div class="form-group">
38+
<input type="submit" value="Change password" class="btn btn-primary" />
39+
</div>
40+
</form>
41+
</div>
42+
</div>
43+
44+
</div>

‎Webgentle.BookStore/Webgentle.BookStore/Views/Shared/_LoginInfo.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</a>
1010
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
1111
<a class="dropdown-item" asp-action="Logout" asp-controller="Account">Logout</a>
12+
<a class="dropdown-item" asp-action="ChangePassword" asp-controller="Account">Change password</a>
1213
</div>
1314
</li>
1415
}

‎Webgentle.BookStore/Webgentle.BookStore/Webgentle.BookStore.csproj.user

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
99
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
1010
<WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth>
11-
<WebStackScaffolding_IsLayoutPageSelected>False</WebStackScaffolding_IsLayoutPageSelected>
11+
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected>
1212
<WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
1313
<WebStackScaffolding_IsReferencingScriptLibrariesSelected>False</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
1414
<WebStackScaffolding_IsAsyncSelected>False</WebStackScaffolding_IsAsyncSelected>
1515
<WebStackScaffolding_ViewDialogWidth>600</WebStackScaffolding_ViewDialogWidth>
1616
<ShowAllFiles>false</ShowAllFiles>
17-
<View_SelectedScaffolderID>RazorViewEmptyScaffolder</View_SelectedScaffolderID>
17+
<View_SelectedScaffolderID>RazorViewScaffolder</View_SelectedScaffolderID>
1818
<View_SelectedScaffolderCategoryPath>root/View</View_SelectedScaffolderCategoryPath>
19+
<WebStackScaffolding_LayoutPageFile />
1920
</PropertyGroup>
2021
</Project>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /