gpt4 book ai didi

asp.net-core - 需要在Asp.Net Core中处理Post Authenticate

转载 作者:行者123 更新时间:2023-12-03 02:20:54 24 4
gpt4 key购买 nike

我已准备好使用 Asp.Net core,但这就是我正在做的事情。在 MVC 5 中,我有一个 Http 模块,它处理 PostAuthenticate 事件,以便创建声明,我在其中执行一些操作来确定用户的角色。我认为在 Core 中没有办法做同样的事情。请注意,这是使用 Windows 身份验证,因此无需处理登录方法。

从当前连接到 PostAuthenticate 的 httpModule 中,因为我想为用户初始化一些东西。
context.PostAuthenticateRequest += Context_PostAuthenticateRequest;

请注意,httpModules 不再存在于 Core 中,并且正在被转移到中间件。不过,我不知道如何从那里利用该事件。

最佳答案

我今天第一次这样做。这里有两个基本步骤。

第一:创建一个实现 IClaimsTransformer 接口(interface)的类。

public class MyTransformer : IClaimsTransformer
{
public Task<ClaimsPrincipal> TransformAsync(ClaimsTransformationContext context )
{
//don't run if user isn't logged in
if(context.Principal.Identity.IsAuthenticated)
{
((ClaimsIdentity)context.Principal.Identity)?.AddClaims(...);
}
}
return Task.FromResult(context.Principal);
}

第二:将此行添加到 Startup.cs 中的

public void Configure(IApplicationBuilder app, ..., ...)
{
//app.Use...Authentication stuff above, for example
app.UseOpenIdConnectAuthentication( new OpenIdOptions
{
//or however you like to do this.
});

app.UseClaimsTransformation(o => new MyTransformer().TransformAsync(o));
//UseMvc below
app.UseMvc(...);
}

请记住,TransformAsync 将针对每个请求运行,因此如果您使用它访问数据库,您可能需要考虑使用 session 或缓存。

关于asp.net-core - 需要在Asp.Net Core中处理Post Authenticate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45043051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com