Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
N
NewLifeProject
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
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
Юнусов Ибрагим
NewLifeProject
Commits
41fa2be8
Commit
41fa2be8
authored
Jul 21, 2022
by
Юнусов Ибрагим
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Добавил администратора
parent
f3393b50
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
55 additions
and
9 deletions
+55
-9
AccountsController.cs
NewStores/Store/Controllers/AccountsController.cs
+1
-0
HomeController.cs
NewStores/Store/Controllers/HomeController.cs
+1
-1
AdminInitializer.cs
NewStores/Store/Helpers/AdminInitializer.cs
+30
-0
Program.cs
NewStores/Store/Program.cs
+22
-7
LoginViewModel.cs
NewStores/Store/ViewModels/Auths/LoginViewModel.cs
+1
-1
No files found.
NewStores/Store/Controllers/AccountsController.cs
View file @
41fa2be8
...
...
@@ -85,6 +85,7 @@ public class AccountsController : Controller
var
result
=
await
_userManager
.
CreateAsync
(
user
,
viewModel
.
Password
);
if
(
result
.
Succeeded
)
{
await
_userManager
.
AddToRoleAsync
(
user
,
"user"
);
await
_signInManager
.
SignInAsync
(
user
,
true
);
return
RedirectToAction
(
"Index"
,
"Home"
);
}
...
...
NewStores/Store/Controllers/HomeController.cs
View file @
41fa2be8
...
...
@@ -19,7 +19,7 @@ public class HomeController : Controller
{
return
View
();
}
[
Authorize
(
Roles
=
"admin"
)]
public
IActionResult
Privacy
()
{
return
View
();
...
...
NewStores/Store/Helpers/AdminInitializer.cs
0 → 100644
View file @
41fa2be8
using
Microsoft.AspNetCore.Identity
;
using
Store.Models.Users
;
namespace
Store.Helpers
;
public
class
AdminInitializer
{
public
static
async
Task
SeedAdminUser
(
RoleManager
<
IdentityRole
>
roleManager
,
UserManager
<
User
>
userManager
)
{
string
adminEmail
=
"admin@admin.com"
;
string
adminPassword
=
"password"
;
var
roles
=
new
[]
{
"admin"
,
"user"
};
foreach
(
var
role
in
roles
)
{
if
(
await
roleManager
.
FindByNameAsync
(
role
)
is
null
)
await
roleManager
.
CreateAsync
(
new
IdentityRole
(
role
));
}
if
(
await
userManager
.
FindByNameAsync
(
adminEmail
)
==
null
)
{
User
admin
=
new
User
{
Email
=
adminEmail
,
UserName
=
adminEmail
};
IdentityResult
result
=
await
userManager
.
CreateAsync
(
admin
,
adminPassword
);
if
(
result
.
Succeeded
)
await
userManager
.
AddToRoleAsync
(
admin
,
"admin"
);
}
}
}
NewStores/Store/Program.cs
View file @
41fa2be8
using
Microsoft.AspNetCore.Authentication.Cookies
;
using
Microsoft.AspNetCore.Identity
;
using
Store.Extensions
;
using
Store.Helpers
;
using
Store.Models
;
using
Store.Models.Users
;
var
builder
=
WebApplication
.
CreateBuilder
(
args
);
var
services
=
builder
.
Services
;
...
...
@@ -14,13 +16,12 @@ services.AddAppServices(configuration);
services
.
Configure
<
IdentityOptions
>(
options
=>
{
// Default Password settings.
options
.
Password
.
RequireDigit
=
true
;
options
.
Password
.
RequireLowercase
=
true
;
options
.
Password
.
RequireNonAlphanumeric
=
true
;
options
.
Password
.
RequireUppercase
=
true
;
options
.
Password
.
RequiredLength
=
6
;
options
.
Password
.
RequiredUniqueChars
=
1
;
options
.
Password
.
RequiredLength
=
5
;
// минимальная длина
options
.
Password
.
RequireNonAlphanumeric
=
false
;
// требуются ли не алфавитно-цифровые символы
options
.
Password
.
RequireLowercase
=
false
;
// требуются ли символы в нижнем регистре
options
.
Password
.
RequireUppercase
=
false
;
// требуются ли символы в верхнем регистре
options
.
Password
.
RequireDigit
=
false
;
// требуются ли цифры
});
services
.
ConfigureApplicationCookie
(
options
=>
...
...
@@ -59,4 +60,18 @@ app.MapControllerRoute(
name
:
"default"
,
pattern
:
"{controller=Accounts}/{action=Login}"
);
using
var
scope
=
app
.
Services
.
CreateScope
();
var
serviceProvider
=
scope
.
ServiceProvider
;
try
{
var
userManager
=
serviceProvider
.
GetRequiredService
<
UserManager
<
User
>>();
var
rolesManager
=
serviceProvider
.
GetRequiredService
<
RoleManager
<
IdentityRole
>>();
await
AdminInitializer
.
SeedAdminUser
(
rolesManager
,
userManager
);
}
catch
(
Exception
ex
)
{
Console
.
WriteLine
(
"An error occurred while seeding the database."
);
}
app
.
Run
();
\ No newline at end of file
NewStores/Store/ViewModels/Auths/LoginViewModel.cs
View file @
41fa2be8
...
...
@@ -10,6 +10,6 @@ public class LoginViewModel
public
string
Password
{
get
;
set
;
}
[
Display
(
Name
=
"Запомнить?"
)]
public
bool
RememberMe
{
get
;
set
;
}
public
string
ReturnUrl
{
get
;
set
;
}
public
string
?
ReturnUrl
{
get
;
set
;
}
}
\ No newline at end of file
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