Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
Lesson49
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Пасько Виталий
Lesson49
Commits
20fd6d08
Commit
20fd6d08
authored
Jul 21, 2022
by
Пасько Виталий
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Добавить авторизацию.
*** - Добавил авторизацию пользователя. ***
parent
79c5f84a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
81 additions
and
4 deletions
+81
-4
AccountController.cs
PhoneStore/Controllers/AccountController.cs
+27
-0
AccountService.cs
PhoneStore/Services/AccountService.cs
+24
-3
Login.cshtml
PhoneStore/Views/Account/Login.cshtml
+29
-0
Register.cshtml
PhoneStore/Views/Account/Register.cshtml
+1
-1
No files found.
PhoneStore/Controllers/AccountController.cs
View file @
20fd6d08
...
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
...
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.AspNetCore.Mvc
;
using
PhoneStore.Enums
;
using
PhoneStore.Enums
;
using
PhoneStore.Helpers
;
using
PhoneStore.Helpers
;
using
PhoneStore.Models
;
using
PhoneStore.Services.Abstractions
;
using
PhoneStore.Services.Abstractions
;
using
PhoneStore.ViewModels.Account
;
using
PhoneStore.ViewModels.Account
;
...
@@ -39,5 +40,31 @@ namespace PhoneStore.Controllers
...
@@ -39,5 +40,31 @@ namespace PhoneStore.Controllers
return
View
(
model
);
return
View
(
model
);
}
}
[
HttpGet
]
public
IActionResult
Login
(
string
returnUrl
=
null
)
{
return
View
(
new
LoginViewModel
{
ReturnUrl
=
returnUrl
});
}
[
HttpPost
]
[
ValidateAntiForgeryToken
]
public
async
Task
<
IActionResult
>
Login
(
LoginViewModel
model
)
{
if
(!
ModelState
.
IsValid
)
return
View
(
model
);
var
result
=
await
_accountService
.
LogIn
(
model
);
if
(
result
.
StatusCodes
==
StatusCodes
.
Success
)
{
if
(!
string
.
IsNullOrEmpty
(
model
.
ReturnUrl
)
&&
Url
.
IsLocalUrl
(
model
.
ReturnUrl
))
return
Redirect
(
model
.
ReturnUrl
);
return
RedirectToAction
(
"Index"
,
"Phones"
);
}
if
(
result
.
ErrorMessages
.
Any
())
ModelState
.
AddErrors
(
result
.
ErrorMessages
);
return
View
(
model
);
}
}
}
}
}
\ No newline at end of file
PhoneStore/Services/AccountService.cs
View file @
20fd6d08
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Linq
;
using
System.Security.Policy
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Microsoft.AspNetCore.Identity
;
using
Microsoft.AspNetCore.Identity
;
using
PhoneStore.Enums
;
using
PhoneStore.Enums
;
...
@@ -24,6 +26,12 @@ namespace PhoneStore.Services
...
@@ -24,6 +26,12 @@ namespace PhoneStore.Services
public
async
Task
<
IdentityResult
>
Register
(
RegisterViewModel
model
)
public
async
Task
<
IdentityResult
>
Register
(
RegisterViewModel
model
)
{
{
if
(
model
is
null
)
return
new
IdentityResult
{
StatusCodes
=
StatusCodes
.
Error
,
ErrorMessages
=
new
List
<
string
>{
"Внутренняя ошибка"
}
};
User
user
=
new
User
User
user
=
new
User
{
{
Email
=
model
.
Email
,
Email
=
model
.
Email
,
...
@@ -44,12 +52,25 @@ namespace PhoneStore.Services
...
@@ -44,12 +52,25 @@ namespace PhoneStore.Services
ErrorMessages
=
errors
,
ErrorMessages
=
errors
,
StatusCodes
=
StatusCodes
.
Error
StatusCodes
=
StatusCodes
.
Error
};
};
}
}
public
Task
<
IdentityResult
>
LogIn
(
LoginViewModel
model
)
public
async
Task
<
IdentityResult
>
LogIn
(
LoginViewModel
model
)
{
{
throw
new
System
.
NotImplementedException
();
User
user
=
await
_userManager
.
FindByEmailAsync
(
model
.
Email
);
SignInResult
result
=
await
_signInManager
.
PasswordSignInAsync
(
user
,
model
.
Password
,
model
.
RememberMe
,
false
);
if
(
result
.
Succeeded
)
return
new
IdentityResult
{
StatusCodes
=
StatusCodes
.
Success
};
return
new
IdentityResult
{
StatusCodes
=
StatusCodes
.
Error
,
ErrorMessages
=
new
List
<
string
>{
"Неправильный логин и (или) пароль"
}
};
}
}
public
Task
<
IdentityResult
>
LogOf
()
public
Task
<
IdentityResult
>
LogOf
()
...
...
PhoneStore/Views/Account/Login.cshtml
0 → 100644
View file @
20fd6d08
@model LoginViewModel
@{
ViewBag.Title = "title";
Layout = "_Layout";
}
<h2>Вход в приложение</h2>
<form method="post" asp-controller="Account" asp-action="Login"
asp-route-returnUrl="@Model.ReturnUrl">
<div asp-validation-summary="ModelOnly"></div>
<div>
<label asp-for="Email"></label><br />
<input asp-for="Email" />
<span asp-validation-for="Email"></span>
</div>
<div>
<label asp-for="Password"></label><br />
<input asp-for="Password" />
<span asp-validation-for="Password"></span>
</div>
<div>
<label asp-for="RememberMe"></label><br />
<input asp-for="RememberMe" />
</div>
<div>
<button type="submit" class="btn btn-outline-warning">Войти</button>
</div>
</form>
PhoneStore/Views/Account/Register.cshtml
View file @
20fd6d08
...
@@ -33,7 +33,7 @@
...
@@ -33,7 +33,7 @@
<span asp-validation-for="PasswordConfirm"></span>
<span asp-validation-for="PasswordConfirm"></span>
</div>
</div>
<div>
<div>
<
input type="submit" value="Регистрация" /
>
<
button type="submit" class="btn btn-outline-warning">Регистрация</button
>
</div>
</div>
</form>
</form>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment