gpt4 book ai didi

c# - 从 AuthenticationHandler 返回自定义对象

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:56 25 4
gpt4 key购买 nike

我正在尝试使用来自第 3 方提供商的自定义身份验证 - 并将其链接到 .net core 2.0。

我已经创建了基础...“ token 认证处理程序”

public class TokenAuthenticationHandler : AuthenticationHandler<TokenAuthenticationOptions>
{
public TokenAuthenticationHandler(IOptionsMonitor<TokenAuthenticationOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock)
: base(options, logger, encoder, clock)
{

}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
// Get the API key
var token = new AuthToken
{
ApiKey = GetKeyValue(Options.ApiKeyName),
Username = GetKeyValue(Options.UsernameKeyName),
Password = GetKeyValue(Options.PasswordKeyName),
IpAddress = Context.Connection.RemoteIpAddress.ToString()
};

// setup the auth repo and identity
var authRepo = new AuthRepository(token);
var identity = new TokenIdentity(authRepo);

// Check the identity
if (identity.IsAuthenticated)
{
var ticket = new AuthenticationTicket(new ClaimsPrincipal(identity), null, "exttoken");
var result = AuthenticateResult.Success(ticket);
return result;
}

// Authentication failed
return AuthenticateResult.NoResult();
}

protected string GetKeyValue(string keyName)
{
return Request
.Headers?
.SingleOrDefault(a => a.Key == keyName)
.Value
.FirstOrDefault();
}
}

“TokenAuthenticationOptions”

public class TokenAuthenticationOptions : AuthenticationSchemeOptions
{
public string ApiKeyName { get; set; } = "X-APIKEY";
public string UsernameKeyName { get; set; } = "X-USERNAME";
public string PasswordKeyName { get; set; } = "X-PASSWORD";
public string CookieName { get; set; } = "MTDATA";
}

这一切都完美无缺,用户是否通过身份验证(通过 401 错误), Controller 被调用...

但是...我需要以某种方式从此处获取“AuthRepository”对象 - 返回到我的 Controller ,因为这是我与第 3 方系统交互的方式。

我试图通过自定义 IIdentity 实现来解决这个问题,如下所示;

public class TokenIdentity : IIdentity
{
public string AuthenticationType { get; } = "exttoken";
public bool IsAuthenticated { get; }
public string Name { get; }
public AuthRepository AuthenticationRepository { get; }

public TokenIdentity(AuthRepository authRepository)
{
AuthenticationRepository = authRepository;
IsAuthenticated = AuthenticationRepository.Authenticate();
if (IsAuthenticated)
Name = AuthenticationRepository.GetCurrentUser()?.Name;
}
}

然后在我的 Controller 中,我尝试使用 HttpContext.User.Identity 获取身份 - 但是此时在 Controller 中,我的客户“TokenIdentity”已转换为“ClaimsPrinciple”,列出的错误是:

System.InvalidCastException: 'Unable to cast object of type 'System.Security.Claims.ClaimsIdentity' to type 'X.X.X.WebAPI.Authentication.TokenIdentity'.'

有什么想法吗?尝试再次调用 authRepository 不是一种选择,因为存在与授权和访问请求相关的开销 - 因此我继续利用现有的 authRepo 对象至关重要。

最佳答案

通过对 HandleAuthenticateAsync 进行以下更改解决了此问题;

// Check the identity
if (identity.IsAuthenticated)
{
var ticket = new AuthenticationTicket(new ClaimsPrincipal(identity), null, "exttoken");
var result = AuthenticateResult.Success(ticket);
Request.HttpContext.Items["auth"] = authRepo;
return result;
}

然后我可以通过以下方式访问 authRepo:

var authRepo = (AuthRepository)HttpContext.Items["auth"];

将此对象存储在 HttpContext.Items 中是个好主意吗?

关于c# - 从 AuthenticationHandler 返回自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46468563/

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