fix: пофиксил ошибку из-за того, что не изменил наследование пользователя на IdentityUser.

parent 20fd6d08
...@@ -66,5 +66,13 @@ namespace PhoneStore.Controllers ...@@ -66,5 +66,13 @@ namespace PhoneStore.Controllers
return View(model); return View(model);
} }
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> LogOff()
{
await _accountService.LogOf();
return RedirectToAction("Index", "Phones");
}
} }
} }
\ No newline at end of file
...@@ -19,6 +19,107 @@ namespace PhoneStore.Migrations ...@@ -19,6 +19,107 @@ namespace PhoneStore.Migrations
.HasAnnotation("ProductVersion", "5.0.17") .HasAnnotation("ProductVersion", "5.0.17")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<int>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("ClaimType")
.HasColumnType("text");
b.Property<string>("ClaimValue")
.HasColumnType("text");
b.Property<int>("RoleId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("RoleId");
b.ToTable("AspNetRoleClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<int>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("ClaimType")
.HasColumnType("text");
b.Property<string>("ClaimValue")
.HasColumnType("text");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("AspNetUserClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<int>", b =>
{
b.Property<string>("LoginProvider")
.HasColumnType("text");
b.Property<string>("ProviderKey")
.HasColumnType("text");
b.Property<string>("ProviderDisplayName")
.HasColumnType("text");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("LoginProvider", "ProviderKey");
b.HasIndex("UserId");
b.ToTable("AspNetUserLogins");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<int>", b =>
{
b.Property<int>("UserId")
.HasColumnType("integer");
b.Property<int>("RoleId")
.HasColumnType("integer");
b.HasKey("UserId", "RoleId");
b.HasIndex("RoleId");
b.ToTable("AspNetUserRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<int>", b =>
{
b.Property<int>("UserId")
.HasColumnType("integer");
b.Property<string>("LoginProvider")
.HasColumnType("text");
b.Property<string>("Name")
.HasColumnType("text");
b.Property<string>("Value")
.HasColumnType("text");
b.HasKey("UserId", "LoginProvider", "Name");
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("PhoneStore.Models.Basket", b => modelBuilder.Entity("PhoneStore.Models.Basket", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
...@@ -103,6 +204,157 @@ namespace PhoneStore.Migrations ...@@ -103,6 +204,157 @@ namespace PhoneStore.Migrations
b.ToTable("Phones"); b.ToTable("Phones");
}); });
modelBuilder.Entity("PhoneStore.Models.Role", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("text");
b.Property<string>("Name")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("NormalizedName")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasDatabaseName("RoleNameIndex");
b.ToTable("AspNetRoles");
});
modelBuilder.Entity("PhoneStore.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("AccessFailedCount")
.HasColumnType("integer");
b.Property<int>("Age")
.HasColumnType("integer");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("text");
b.Property<string>("Email")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<bool>("EmailConfirmed")
.HasColumnType("boolean");
b.Property<bool>("LockoutEnabled")
.HasColumnType("boolean");
b.Property<DateTimeOffset?>("LockoutEnd")
.HasColumnType("timestamp with time zone");
b.Property<string>("Name")
.HasColumnType("text");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("NormalizedUserName")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("PasswordHash")
.HasColumnType("text");
b.Property<string>("PhoneNumber")
.HasColumnType("text");
b.Property<bool>("PhoneNumberConfirmed")
.HasColumnType("boolean");
b.Property<string>("SecurityStamp")
.HasColumnType("text");
b.Property<bool>("TwoFactorEnabled")
.HasColumnType("boolean");
b.Property<string>("UserName")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.HasKey("Id");
b.HasIndex("NormalizedEmail")
.HasDatabaseName("EmailIndex");
b.HasIndex("NormalizedUserName")
.IsUnique()
.HasDatabaseName("UserNameIndex");
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<int>", b =>
{
b.HasOne("PhoneStore.Models.Role", null)
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<int>", b =>
{
b.HasOne("PhoneStore.Models.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<int>", b =>
{
b.HasOne("PhoneStore.Models.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<int>", b =>
{
b.HasOne("PhoneStore.Models.Role", null)
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PhoneStore.Models.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<int>", b =>
{
b.HasOne("PhoneStore.Models.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("PhoneStore.Models.Basket", b => modelBuilder.Entity("PhoneStore.Models.Basket", b =>
{ {
b.HasOne("PhoneStore.Models.Phone", "Phone") b.HasOne("PhoneStore.Models.Phone", "Phone")
......
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace PhoneStore.Models namespace PhoneStore.Models
{ {
public class MobileContext : DbContext public class MobileContext : IdentityDbContext<User, Role, int>
{ {
public DbSet<Phone> Phones { get; set; } public DbSet<Phone> Phones { get; set; }
public DbSet<Order> Orders { get; set; } public DbSet<Order> Orders { get; set; }
......
...@@ -24,7 +24,7 @@ namespace PhoneStore ...@@ -24,7 +24,7 @@ namespace PhoneStore
{ {
var userManager = services.GetRequiredService<UserManager<User>>(); var userManager = services.GetRequiredService<UserManager<User>>();
RoleManager<Role> rolesManager = services.GetRequiredService<RoleManager<Role>>(); RoleManager<Role> rolesManager = services.GetRequiredService<RoleManager<Role>>();
await AdminInitializer.SeedAdminUser(rolesManager, userManager); await AdminInitializerService.SeedAdminUser(rolesManager, userManager);
} }
catch (Exception ex) catch (Exception ex)
{ {
......
...@@ -8,6 +8,6 @@ namespace PhoneStore.Services.Abstractions ...@@ -8,6 +8,6 @@ namespace PhoneStore.Services.Abstractions
{ {
Task<IdentityResult> Register(RegisterViewModel model); Task<IdentityResult> Register(RegisterViewModel model);
Task<IdentityResult> LogIn(LoginViewModel model); Task<IdentityResult> LogIn(LoginViewModel model);
Task<IdentityResult> LogOf(); Task LogOf();
} }
} }
\ No newline at end of file
using System.Collections.Generic; 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;
...@@ -73,9 +72,7 @@ namespace PhoneStore.Services ...@@ -73,9 +72,7 @@ namespace PhoneStore.Services
}; };
} }
public Task<IdentityResult> LogOf() public async Task LogOf()
{ => await _signInManager.SignOutAsync();
throw new System.NotImplementedException();
}
} }
} }
\ No newline at end of file
...@@ -4,7 +4,7 @@ using PhoneStore.Models; ...@@ -4,7 +4,7 @@ using PhoneStore.Models;
namespace PhoneStore.Services namespace PhoneStore.Services
{ {
public class AdminInitializer public class AdminInitializerService
{ {
public static async Task SeedAdminUser( public static async Task SeedAdminUser(
RoleManager<Role> roleManager, RoleManager<Role> roleManager,
......
...@@ -50,7 +50,7 @@ namespace PhoneStore ...@@ -50,7 +50,7 @@ namespace PhoneStore
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
app.UseAuthorization().UseAuthentication();
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapControllerRoute( endpoints.MapControllerRoute(
......
...@@ -29,6 +29,21 @@ ...@@ -29,6 +29,21 @@
</li> </li>
</ul> </ul>
</div> </div>
<div class="login_group">
@if(User.Identity.IsAuthenticated)
{
<p>@User.Identity.Name</p>
<form method="post" asp-controller="Account" asp-action="LogOff">
<input type="submit" value="Выход" />
</form>
}
else
{
<a asp-controller="Account" asp-action="Login">Вход</a>
<a asp-controller="Account" asp-action="Register">Регистрация</a>
}
</div>
</div> </div>
</nav> </nav>
</header> </header>
......
{ {
"ConnectionStrings": { "ConnectionStrings": {
"DefaultConnection": "Server=localhost;Port=54320;database=lesson55;Username=postgres;Password=postgres;" "DefaultConnection": "Server=localhost;Port=54320;database=lesson58;Username=postgres;Password=postgres;"
}, },
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment