我从 Visual Studio 模板创建了一个全新的 WebAPI 项目。目标框架netcoreapp3.1。我已经使用windows 身份验证对其进行了配置。我使用 NTLM 配置在 Postman 中创建了一个请求来调用我的 API。当我调试我的应用程序并通过 Postman 调用请求时,我收到以下错误:
IIS 10.0 Detailed Error - 401.1 - Unauthorized
我的创业班:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace WebApplication1
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. …Run Code Online (Sandbox Code Playgroud) windows-authentication postman asp.net-core-webapi ntlm-authentication
我想在自定义属性中实现本地化,以检查该属性是否是有效的 IP 地址或主机名。到目前为止,验证工作正常,但我的问题是,尽管我的本地切换为德语,但我只收到默认的英语错误消息。我正在处理资源文件。我不想为此实现客户端验证。我知道有实现适配器的方法,但如果我错了,请纠正我,这仅用于客户端验证。
我的自定义验证类:
public class IPAddressOrHostnameAttribute : ValidationAttribute
{
public IPAddressOrHostnameAttribute(string propertyName, object desiredvalue, string errorMessage)
{
PropertyName = propertyName;
DesiredValue = desiredvalue;
ErrorMessage = errorMessage;
}
private string PropertyName { get; }
private object DesiredValue { get; }
protected override ValidationResult IsValid(object value, ValidationContext context)
{
var instance = context.ObjectInstance;
var type = instance.GetType();
var propertyValue = type.GetProperty(PropertyName).GetValue(instance, null);
if (propertyValue.ToString() == DesiredValue.ToString() && value != null)
{
if (Regex.IsMatch(value.ToString(), AckConstants.VALIDIPADDRESSREGEX)
|| Regex.IsMatch(value.ToString(), AckConstants.VALIDHOSTNAMEREGEX))
{
return ValidationResult.Success;
} …Run Code Online (Sandbox Code Playgroud)