add exception filter when user not connected default to needing authentication and apply anonymous to some actions add user in get requests add user relation in link, comment and vote signup and in are ok now!
32 lines
810 B
C#
32 lines
810 B
C#
using System;
|
|
using HN.Application;
|
|
using HN.Infrastructure;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
namespace Website
|
|
{
|
|
public sealed class HttpExecutingUserProvider : IExecutingUserProvider
|
|
{
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly UserManager<User> _userManager;
|
|
|
|
public HttpExecutingUserProvider(IHttpContextAccessor httpContextAccessor, UserManager<User> userManager)
|
|
{
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_userManager = userManager;
|
|
}
|
|
|
|
public Guid GetCurrentUserId()
|
|
{
|
|
var uid = _userManager.GetUserId(_httpContextAccessor.HttpContext.User);
|
|
|
|
if (!Guid.TryParse(uid, out Guid result))
|
|
{
|
|
throw new UserNotConnected();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |