[英]Register MessageHandler in Program.cs
I am trying to build the ASP.NET Core Web API from this link ( https://docs.microsoft.com/en-us/azure/notification-hubs/push-notifications-android-specific-users-firebase-cloud-messaging ). I am trying to build the ASP.NET Core Web API from this link ( https://docs.microsoft.com/en-us/azure/notification-hubs/push-notifications-android-specific-users-firebase-cloud-messaging ) . It wants me to register the MessageHandler using the following code:它希望我使用以下代码注册 MessageHandler:
config.MessageHandlers.Add(new AuthenticationTestHandler());
It says to "add the following code at the end of the Register method in the Program.cs file. There is no register method in the Program.cs file. Everything I have found indicates that the Register method is part of a class called WebApiConfig
, but I don't have one of those either, and when I create one, it cannot find HttpConfiguration. How do I register the MessageHandler?它说“在Program.cs文件中 Register 方法的末尾添加以下代码。Program.cs文件中没有 register 方法。我发现的所有内容都表明 Register 方法是 class 的一部分,称为WebApiConfig
,但是我也没有,创建的时候找不到HttpConfiguration,请问如何注册MessageHandler?
In ASP.NET Core the equivalent to a MessageHandler
is middleware.在 ASP.NET Core 中,相当于MessageHandler
的是中间件。 I converted AuthenticationTestHandler
to middleware:我将AuthenticationTestHandler
转换为中间件:
AuthenticationTestMiddleware.cs: AuthenticationTestMiddleware.cs:
public class AuthenticationTestMiddleware
{
private readonly RequestDelegate _next;
public AuthenticationTestMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
var authorizationHeader = context.Request.Headers["Authorization"].FirstOrDefault();
if (authorizationHeader != null && authorizationHeader
.StartsWith("Basic ", StringComparison.InvariantCultureIgnoreCase))
{
string authorizationUserAndPwdBase64 =
authorizationHeader.Substring("Basic ".Length);
string authorizationUserAndPwd = Encoding.Default
.GetString(Convert.FromBase64String(authorizationUserAndPwdBase64));
string user = authorizationUserAndPwd.Split(':')[0];
string password = authorizationUserAndPwd.Split(':')[1];
if (VerifyUserAndPwd(user, password))
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, user)
};
var claimsIdentity = new ClaimsIdentity(
claims: claims,
authenticationType: "password");
context.User.AddIdentity(claimsIdentity);
await _next(context);
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
}
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
}
}
private bool VerifyUserAndPwd(string user, string password)
{
// This is not a real authentication scheme.
return user == password;
}
}
Now in Program.cs
you can register like this:现在在Program.cs
中,您可以像这样注册:
app.UseMiddleware<AuthenticationTestMiddleware>();
Questions I found regarding converting MessageHandler
to middleware:我发现关于将MessageHandler
转换为中间件的问题:
DelegateHandler from ASP.NET to .NET Core 从 ASP.NET 到 .NET 核心的 DelegateHandler
Registering a new DelegatingHandler in ASP.NET Core Web API 在 ASP.NET 内核中注册一个新的 DelegatingHandler Web API
is there any class something similar to DelegatingHandler in asp.net core web api.? 在 asp.net 核心 web Z8A5DA52ED12605721D35A8E70A 中是否有任何 class 类似于 DelegatingHandler 的东西?
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-6.0 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-6.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.