74 lines
2.3 KiB
C#
74 lines
2.3 KiB
C#
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
using Microsoft.AspNetCore.Http;
|
|||
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using Syc.Abp.Application.Contracts;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Volo.Abp.Application.Services;
|
|||
|
|
|||
|
[Authorize]
|
|||
|
public abstract class ApiService : ApplicationService
|
|||
|
{
|
|||
|
public ApiService()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public ApiService(ILogger logger)
|
|||
|
{
|
|||
|
Logger = logger;
|
|||
|
}
|
|||
|
|
|||
|
private HttpContext _httpContext { get; set; }
|
|||
|
protected HttpContext HttpContext
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_httpContext is null)
|
|||
|
_httpContext = base.LazyServiceProvider.LazyGetService<IHttpContextAccessor>()?.HttpContext;
|
|||
|
return _httpContext;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private Identity _identity { get; set; }
|
|||
|
protected Identity Identity
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_identity is null)
|
|||
|
_identity = GetCurrentUser();
|
|||
|
return _identity;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
protected ILogger Logger { get; }
|
|||
|
|
|||
|
private Identity GetCurrentUser()
|
|||
|
{
|
|||
|
var claims = HttpContext.User?.Claims;
|
|||
|
if (claims is null || !claims.Any()) return null;
|
|||
|
if (_identity is null)
|
|||
|
{
|
|||
|
int.TryParse(claims.FirstOrDefault(e => e.Type == nameof(Identity.Id)).Value, out int id);
|
|||
|
_identity = new Identity()
|
|||
|
{
|
|||
|
Id = id,
|
|||
|
Account = claims.FirstOrDefault(e => e.Type == nameof(Identity.Account))?.Value,
|
|||
|
Email = claims.FirstOrDefault(e => e.Type == nameof(Identity.Email))?.Value,
|
|||
|
NickName = claims.FirstOrDefault(e => e.Type == nameof(Identity.NickName))?.Value,
|
|||
|
Organizations = claims.FirstOrDefault(e => e.Type == nameof(Identity.Organizations))?.Value,
|
|||
|
Positions = claims.FirstOrDefault(e => e.Type == nameof(Identity.Positions))?.Value,
|
|||
|
RealName = claims.FirstOrDefault(e => e.Type == nameof(Identity.RealName))?.Value,
|
|||
|
Roles = claims.FirstOrDefault(e => e.Type == nameof(Identity.Roles))?.Value,
|
|||
|
Telephone = claims.FirstOrDefault(e => e.Type == nameof(Identity.Telephone))?.Value
|
|||
|
};
|
|||
|
}
|
|||
|
return Identity;
|
|||
|
}
|
|||
|
}
|