-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.cs
More file actions
32 lines (27 loc) · 1.04 KB
/
UserService.cs
File metadata and controls
32 lines (27 loc) · 1.04 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
using System;
using System.Globalization;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
namespace QueryFilterServiceApp.Services {
public interface IUserService {
int GetCurrentUserId();
string GetCurrentUserName();
}
public class UserService : IUserService {
readonly IHttpContextAccessor contextAccessor;
public UserService(IHttpContextAccessor contextAccessor) {
this.contextAccessor = contextAccessor
?? throw new ArgumentNullException(nameof(contextAccessor));
}
public int GetCurrentUserId() {
var sidStr = contextAccessor.HttpContext.User.Claims
.FirstOrDefault(x => x.Type == ClaimTypes.Sid);
return Convert.ToInt32(sidStr.Value, CultureInfo.InvariantCulture);
}
public string GetCurrentUserName() {
return contextAccessor.HttpContext.User.Claims
.Single(x => x.Type == ClaimTypes.Name).Value;
}
}
}