Commit 4e78a832 authored by Yevgeniy Agrafenin's avatar Yevgeniy Agrafenin

#2 Добавил контекст базы данных, изменил шаблон главной страницы.

parent 47626667
using CafeCritic.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace CafeCritic.Context;
public class CafeDbContext: IdentityDbContext<User>
{
CafeDbContext(DbContextOptions options) : base(options){}
}
\ No newline at end of file
using CafeCritic.Context;
using CafeCritic.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
...@@ -5,6 +10,20 @@ builder.Services.AddControllersWithViews(); ...@@ -5,6 +10,20 @@ builder.Services.AddControllersWithViews();
var app = builder.Build(); var app = builder.Build();
var connectionString = builder.Configuration
.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<CafeDbContext>(options => options.UseNpgsql(connectionString)).AddIdentity<User, IdentityRole>(
o =>
{
o.User.RequireUniqueEmail = true;
o.Password.RequiredLength = 6;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireDigit = false;
}).AddEntityFrameworkStores<CafeDbContext>();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
...@@ -18,6 +37,7 @@ app.UseStaticFiles(); ...@@ -18,6 +37,7 @@ app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.MapControllerRoute( app.MapControllerRoute(
......
<!DOCTYPE html> @using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Mvc.TagHelpers
@inject UserManager<User> UserManager;
@{
var user = await UserManager.GetUserAsync(User);
}
<!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"/> <meta charset="utf-8"/>
...@@ -20,11 +26,28 @@ ...@@ -20,11 +26,28 @@
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between"> <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1"> <ul class="navbar-nav flex-grow-1">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a> <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Cafe Critic</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li> </li>
@if (User.Identity!.IsAuthenticated)
{
<li class="nav-item">
<span class="nav-link text-dark">Привет - @user.Email</span>
</li>
<li class="nav-item">
<form asp-action="LogOut" asp-controller="Account" method="post">
<button type="submit" class="btn btn-dark">Выйти</button>
</form>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-action="Login" asp-controller="Account">Войти</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-action="Register" asp-controller="Account">Зарегистрироваться</a>
</li>
}
</ul> </ul>
</div> </div>
</div> </div>
...@@ -38,7 +61,7 @@ ...@@ -38,7 +61,7 @@
<footer class="border-top footer text-muted"> <footer class="border-top footer text-muted">
<div class="container"> <div class="container">
&copy; 2023 - CafeCritic - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a> &copy; 2023 - Cafe Critic
</div> </div>
</footer> </footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script> <script src="~/lib/jquery/dist/jquery.min.js"></script>
......
{ {
"ConnectionStrings": {
"DefaultConnection" : "Server=127.0.0.1;Port=5432;Database=CafeCritic;User Id=postgres;Password=attractor01"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
......
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