-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAuthExtensions.cs
More file actions
58 lines (51 loc) · 1.96 KB
/
AuthExtensions.cs
File metadata and controls
58 lines (51 loc) · 1.96 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
56
57
58
using System.Linq;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.AuthService.ViewModels;
using HwProj.Models.Roles;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
namespace HwProj.Utils.Authorization
{
public static class AuthExtensions
{
public static string? GetUserIdFromHeader(this HttpRequest request) =>
request.Headers.TryGetValue("UserId", out var id) ? id.FirstOrDefault() : null;
public static string? GetGuestModeFromHeader(this HttpRequest request) =>
request.Headers.TryGetValue("GuestMode", out var isGuest) ? isGuest.FirstOrDefault() : null;
public static AccountDataDto ToAccountDataDto(this User user, string role)
{
return new AccountDataDto(
user.Id,
user.Name,
user.Surname,
user.Email,
role,
user.IsExternalAuth,
user.MiddleName,
user.GitHubId);
}
public static string GetUserName(this HttpRequest request)
{
return request.Query.First(x => x.Key == "_userName").Value.ToString();
}
public static string GetUserRole(this HttpRequest request)
{
var claimRole = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
var role = request.HttpContext.User.Claims.FirstOrDefault(claim => claim.Type.ToString() == claimRole);
return role == null
? null
: role.Value;
}
public static bool IsLecturer(this string role)
{
return role == Roles.LecturerRole;
}
public static string GetMentorId(this HttpRequest request)
{
request.HttpContext.Request.Headers.TryGetValue("UserId", out var userId);
return StringValues.IsNullOrEmpty(userId)
? null
: userId.ToString();
}
}
}