46 lines
1.8 KiB
C#
46 lines
1.8 KiB
C#
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.Encodings.Web;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sarand.BusinessMgr.Net.Core.Web.Middleware
|
|
{
|
|
//=======================================================================
|
|
// Authentication handler required to allow actions to issue Forbid() responses.
|
|
// Ref. https://stackoverflow.com/questions/54578426/why-do-i-need-to-set-a-defaultforbidscheme/54587054#54587054
|
|
//=======================================================================
|
|
public class SbmAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
|
|
//-------------------------------------------------------------------
|
|
public SbmAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) :
|
|
base(options, logger, encoder, clock)
|
|
{
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
protected override Task HandleForbiddenAsync(AuthenticationProperties properties)
|
|
{
|
|
Context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
|
|
return base.HandleForbiddenAsync(properties);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
{
|
|
Context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
|
return AuthenticateResult.NoResult();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|