Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
CafeCritic
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
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
Yevgeniy Agrafenin
CafeCritic
Commits
47626667
Commit
47626667
authored
Nov 18, 2023
by
Yevgeniy Agrafenin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#2
Добавил AccountController.cs и представления входа и регистрации Login.cshtml, Register.cshtml.
parent
e5ee2aee
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
182 additions
and
0 deletions
+182
-0
AccountController.cs
CafeCritic/Controllers/AccountController.cs
+101
-0
Login.cshtml
CafeCritic/Views/Account/Login.cshtml
+40
-0
Register.cshtml
CafeCritic/Views/Account/Register.cshtml
+41
-0
No files found.
CafeCritic/Controllers/AccountController.cs
0 → 100644
View file @
47626667
using
CafeCritic.Models
;
using
CafeCritic.ViewModels
;
using
Microsoft.AspNetCore.Identity
;
using
Microsoft.AspNetCore.Mvc
;
namespace
CafeCritic.Controllers
;
public
class
AccountController
:
Controller
{
private
readonly
UserManager
<
User
>
_userManager
;
private
readonly
SignInManager
<
User
>
_signInManager
;
public
AccountController
(
UserManager
<
User
>
userManager
,
SignInManager
<
User
>
signInManager
)
{
_userManager
=
userManager
;
_signInManager
=
signInManager
;
}
// GET
[
HttpGet
]
public
IActionResult
Register
()
{
return
View
();
}
[
HttpPost
]
public
async
Task
<
IActionResult
>
Register
(
RegisterViewModel
model
)
{
if
(
ModelState
.
IsValid
)
{
var
user
=
new
User
{
Email
=
model
.
Email
};
var
result
=
await
_userManager
.
CreateAsync
(
user
,
model
.
Password
);
if
(
result
.
Succeeded
)
{
await
_signInManager
.
SignInAsync
(
user
,
false
);
return
RedirectToAction
(
"Index"
,
"Home"
);
}
foreach
(
var
error
in
result
.
Errors
)
{
ModelState
.
AddModelError
(
string
.
Empty
,
error
.
Description
);
}
}
return
View
(
"Register"
,
model
);
}
[
HttpGet
]
public
IActionResult
Login
(
string
returnUrl
=
null
)
{
return
View
(
new
LoginViewModel
{
ReturnUrl
=
returnUrl
});
}
[
HttpPost
]
public
async
Task
<
IActionResult
>
Login
(
LoginViewModel
model
)
{
if
(
ModelState
.
IsValid
)
{
var
userName
=
model
.
Email
;
if
(
userName
!=
null
&&
userName
.
IndexOf
(
'@'
)
>
-
1
)
{
User
user
=
await
_userManager
.
FindByEmailAsync
(
model
.
Email
);
if
(
user
==
null
)
{
ModelState
.
AddModelError
(
string
.
Empty
,
"Войти не удалось"
);
return
View
(
model
);
}
userName
=
user
.
UserName
;
}
var
result
=
await
_signInManager
.
PasswordSignInAsync
(
userName
,
model
.
Password
,
model
.
RememberMe
,
false
);
if
(
result
.
Succeeded
)
{
if
(!
string
.
IsNullOrEmpty
(
model
.
ReturnUrl
)
&&
Url
.
IsLocalUrl
(
model
.
ReturnUrl
))
{
return
Redirect
(
model
.
ReturnUrl
);
}
return
RedirectToAction
(
"Index"
,
"Home"
);
}
ModelState
.
AddModelError
(
string
.
Empty
,
"Неправильный логин и (или) пароль"
);
}
return
View
(
model
);
}
[
HttpPost
]
public
async
Task
<
IActionResult
>
LogOut
()
{
await
_signInManager
.
SignOutAsync
();
return
RedirectToAction
(
"Index"
,
"Home"
);
}
}
\ No newline at end of file
CafeCritic/Views/Account/Login.cshtml
0 → 100644
View file @
47626667
@model CafeCritic.ViewModels.LoginViewModel
@{
ViewBag.Title = "Вход";
Layout = "_Layout";
}
<div class="row">
<div class="col-12">
<div class="card w-50">
<div class="card-body">
<form asp-action="Login" method="post" asp-antiforgery="true">
<div class="validation" asp-validation-summary="All"></div>
<div class="mb-3">
<input asp-for="Email" class="form-control" placeholder="Введите Email">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="mb-3">
<input asp-for="Password" class="form-control" placeholder="Пароль">
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<label asp-for="RememberMe" class="form-label"></label>
<input asp-for="RememberMe" type="checkbox">
<input asp-for="ReturnUrl" type="hidden"><br/>
<button class="btn btn-primary" type="submit">Вход</button>
<p class="card-body">
<a asp-action="Register">Регистрация!</a>
</p>
</form>
</div>
</div>
</div>
</div>
@section Scripts
{
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
CafeCritic/Views/Account/Register.cshtml
0 → 100644
View file @
47626667
@model dynamic
@{
ViewBag.Title = "Регистрация";
Layout = "_Layout";
}
<div class="row">
<div class="col-12">
<div class="card w-50">
<div class="card-body">
<form asp-action="Register" method="post" asp-antiforgery="true">
<div class="validation" asp-validation-summary="All"></div>
<div class="mb-3">
<input asp-for="Email" class="form-control" placeholder="Email">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="mb-3">
<input asp-for="Password" class="form-control" placeholder="Пароль">
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="mb-3">
<input asp-for="ConfirmPassword" class="form-control" placeholder="Повторите пароль">
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
<button class="btn btn-primary" type="submit">Зарегистрироваться</button>
<p class="card-body">
<a asp-action="Login">Войти</a>
</p>
</form>
</div>
</div>
</div>
</div>
@section Scripts
{
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
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