C# .Net 5 Windows和自定义身份验证
我正在写一个运行在.NET5.0上的RazorPages应用程序。此应用程序需要能够支持员工(使用Windows身份验证登录)和申请人(没有Windows帐户,因此需要使用自定义身份验证过程注册/登录)。我可以让Windows auth或自定义auth工作,但这两个都不想一起玩 我认为我需要编写C# .Net 5 Windows和自定义身份验证,c#,.net-core,razor-pages,C#,.net Core,Razor Pages,我正在写一个运行在.NET5.0上的RazorPages应用程序。此应用程序需要能够支持员工(使用Windows身份验证登录)和申请人(没有Windows帐户,因此需要使用自定义身份验证过程注册/登录)。我可以让Windows auth或自定义auth工作,但这两个都不想一起玩 我认为我需要编写iaauthenticationservice的自定义实现,但这正是它绊倒我的地方。我无法确定在ChallengeAsync中需要做什么才能让挑战通过 下面是AuthService目前的实现(是的,这不是
iaauthenticationservice
的自定义实现,但这正是它绊倒我的地方。我无法确定在ChallengeAsync
中需要做什么才能让挑战通过
下面是AuthService目前的实现(是的,这不是最好的,但我现在的重点是让它工作!!):
公共类AuthService:IAAuthenticationService
{
异步任务IAAuthenticationService.AuthenticateTasync(HttpContext上下文,字符串方案)
{
if(HasAnonymousAttribute(上下文))
{
返回AuthenticateResult.NoResult();
}
var user=getUser(上下文);
如果(用户!=null)
{
var票证=新的身份验证票证(用户,“魔术”);
返回AuthenticateResult.Success(票证);
}
等待上下文。ChallengeAsync(“Windows”);
if(context.User.Identity.IsAuthenticated)
{
var票证=新身份验证票证(context.User,“Windows”);
返回AuthenticateResult.Success(票证);
}
返回AuthenticateResult.Fail(“请登录”);
}
任务IAAuthenticationService.ChallengeAsync(HttpContext上下文、字符串方案、AuthenticationProperties)
{
var user=context.Session.Get(“用户”);
if(user==null)
{
//采取措施阻止用户访问
}
返回Task.FromResult(0);
}
任务IAAuthenticationService.Async(HttpContext上下文、字符串方案、AuthenticationProperties)
{
抛出新的NotImplementedException();
}
任务IAAuthenticationService.SignInAsync(HttpContext上下文、字符串方案、ClaimsPrincipal主体、AuthenticationProperties)
{
if(scheme.ToLower()=“magic”)
{
context.Session.Set(“User”,Encoding.ASCII.GetBytes(principal.Identity.Name));
}
返回Task.FromResult(0);
}
任务IAAuthenticationService.SignOutAsync(HttpContext上下文、字符串方案、AuthenticationProperties)
{
抛出新的NotImplementedException();
}
private ClaimsPrincipal getUser(HttpContext上下文)
{
if(context.User.Identity.IsAuthenticated)
{
返回(ClaimsPrincipal)context.User.Identity;
}
返回null;
}
私有bool hasnonymousAttribute(HttpContext上下文)
{
var endpoint=context.GetEndpoint();
var retVal=(端点?.Metadata?.GetMetadata()!=null);
返回返回;
}
}
您应该只实现需要自定义逻辑的方法
因此,您可以将常规Asp.NetAuthenticationService
子类化,然后将自定义逻辑放入AuthenticationAsync
中,而不是实现接口,但不要覆盖ChallengeAsync
using Microsoft.AspNetCore.Authentication;
namespace Some.Lovely.Namespace
{
public class MyCustomAuthenticationService : AuthenticationService
{
public CustomAuthenticationService(
[NotNull] IAuthenticationSchemeProvider schemes,
[NotNull] IAuthenticationHandlerProvider handlers,
[NotNull] IClaimsTransformation transform,
[NotNull][ItemNotNull] IOptions<AuthenticationOptions> options) :
base(schemes, handlers, transform, options)
{ }
public override async Task<AuthenticateResult> AuthenticateAsync(HttpContext context, string? scheme)
{
// your custom logic
}
}
}
using Microsoft.AspNetCore.Authentication;
namespace Some.Lovely.Namespace
{
public class MyCustomAuthenticationService : AuthenticationService
{
public CustomAuthenticationService(
[NotNull] IAuthenticationSchemeProvider schemes,
[NotNull] IAuthenticationHandlerProvider handlers,
[NotNull] IClaimsTransformation transform,
[NotNull][ItemNotNull] IOptions<AuthenticationOptions> options) :
base(schemes, handlers, transform, options)
{ }
public override async Task<AuthenticateResult> AuthenticateAsync(HttpContext context, string? scheme)
{
// your custom logic
}
}
}
services.AddScoped<IAuthenticationService, MyCustomAuthenticationService >();