-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobal.asax.cs
More file actions
executable file
·55 lines (46 loc) · 1.84 KB
/
Global.asax.cs
File metadata and controls
executable file
·55 lines (46 loc) · 1.84 KB
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
using Shop.Models.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace Shop
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
// Создаем метод обработки запросов аутентификации
protected void Application_AuthenticateRequest()
{
// Проверяем, что пользователь авторизован
if (User == null)
return;
// Получаем имя пользователя
string userName = Context.User.Identity.Name;
// Объявляем массив ролей
string[] roles = null;
using (Db db = new Db())
{
// Заполняем роли
UserDTO dto = db.Users.FirstOrDefault(x => x.Username == userName);
if (dto == null)
return;
roles = db.UserRoles.Where(x => x.UserId == dto.Id).Select(x => x.Role.Name).ToArray();
}
// Создаем объект интерфейса IPrincipal
IIdentity userIdentity = new GenericIdentity(userName);
IPrincipal newUserObj = new GenericPrincipal(userIdentity, roles);
// Объявляем и инициализируем данными Context.User
Context.User = newUserObj;
}
}
}