-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProgram.cs
162 lines (131 loc) · 5.5 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("SQLDBConnection");
builder.Services.AddDbContext<DevNotContext>(x => x.UseSqlServer(connectionString));
builder.Services.AddScoped<IZipService, ZipService>();
builder.Services.AddDataProtection();
builder.Services.AddCors();
var serviceProvider = builder.Services.BuildServiceProvider();
var _provider = serviceProvider.GetService<IDataProtectionProvider>();
var protector = _provider.CreateProtector(builder.Configuration["Protector_Key"]);
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new DevNotMapper(protector));
});
IMapper autoMapper = mappingConfig.CreateMapper();
builder.Services.AddSingleton(autoMapper);
builder.Services.AddAuthorization();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateActor = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Issuer"],
ValidAudience = builder.Configuration["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["SigningKey"]))
};
});
builder.Services.AddSingleton<ITokenService>(new TokenService(builder));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Api", Version = "v1" });
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}});
});
var app = builder.Build();
app.UseAuthorization();
app.UseAuthentication();
app.UseCors(p =>
{
p.AllowAnyOrigin();
p.WithMethods("GET");
p.AllowAnyHeader();
});
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api v1"));
}
app.MapGet("/", () => "Hello World!");
app.MapGet("/GetRoles", (Func<List<Role>>)(() => new()
{
new(1, "Admin", 1),
new(2, "User", 1),
new(3, "Worker", 1)
}));
app.MapGet("/GetTop5UserPermisions", async (DevNotContext context, IZipService service) =>
{
var userList = await context.DbUser.Where(du => du.IdSecurityRole != null).
Select(u => $"{u.Name} {u.LastName}").Take(5).ToListAsync();
var roleList = await (from role in context.DbSecurityRole
join user in context.DbUser
on role.IdSecurityRole equals user.IdSecurityRole
select role.SecurityRoleName).Take(5).ToListAsync();
var IdUserList = await context.DbUser.Where(du => du.IdSecurityRole != null).
Select(db => db.IdUser.ToString()).Take(5).ToListAsync();
var actionList = await context.DbSecurityUserAction.Where(a => a.IdSecurityController == 2 &&
IdUserList.Contains(a.IdUser.ToString())).Select(u => u.ActionNumberTotal).Take(5).ToListAsync();
return service.ZipResult(userList, roleList, actionList);
});
app.MapPost("/InsertUser", async (DevNotContext context, User user,IMapper mapper) =>
{
//var model = autoMapper.Map<DbUser>(user);
var model = mapper.Map<DbUser>(user);
context.DbUser.Add(model);
await context.SaveChangesAsync();
return new OkResult();
});
app.MapGet("/GetAllUsersByName/{name}", [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] async (HttpContext http, DevNotContext context, string name) =>
{
var sqlQuery = context.DbUser.Where(u => u.UserName.Contains(name)).ToQueryString();
System.Diagnostics.Debug.WriteLine(sqlQuery);
var model = await context.DbUser.Where(u => u.UserName.Contains(name)).ToListAsync();
var result = autoMapper.Map<List<User>>(model);
//result.ForEach(user => { user.PasswordHash = user.PasswordHash == null ? "" : protector.Unprotect(user.PasswordHash); });
return result;
});//.RequireAuthorization();
app.MapPost("/login", [AllowAnonymous] async (DevNotContext _context, HttpContext http, ITokenService service, Login login) =>
{
if (!string.IsNullOrEmpty(login.UserName) &&
!string.IsNullOrEmpty(login.Password))
{
var userModel = await _context.DbUser.Where(u => u.UserName == login.UserName && u.Password == login.Password).FirstOrDefaultAsync();
if (userModel == null)
{
http.Response.StatusCode = 401;
await http.Response.WriteAsJsonAsync(new { Message = "Yor Are Not Authorized!" });
return;
}
var token = service.GetToken(userModel);
await http.Response.WriteAsJsonAsync(new { token = token });
return;
}
});
//app.Urls.Add("https://localhost:1923");
app.Run();