DocSpace-buildtools/web/ASC.Web.Core/CookieAuthHandler.cs

38 lines
1.5 KiB
C#
Raw Normal View History

2019-05-30 14:57:15 +00:00
using System.Net;
using System.Security.Authentication;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using ASC.Core;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
2019-07-09 10:29:53 +00:00
namespace ASC.Web.Core
{
public class CookieAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
public CookieAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}
2019-08-15 12:04:42 +00:00
public CookieAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IHttpContextAccessor httpContextAccessor)
: this(options, logger, encoder, clock)
{
2019-06-06 08:44:38 +00:00
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
2019-05-30 15:21:30 +00:00
var token = Context.Request.Cookies["asc_auth_key"] ?? Context.Request.Headers["Authorization"];
var result = SecurityContext.AuthenticateMe(token);
2019-05-31 10:47:47 +00:00
return Task.FromResult(
result ?
AuthenticateResult.Success(new AuthenticationTicket(Context.User, new AuthenticationProperties(), Scheme.Name)) :
AuthenticateResult.Fail(new AuthenticationException(HttpStatusCode.Unauthorized.ToString()))
);
}
}
}