C# 如何通过子域访问我的web api
我目前正在使用.NET做一个web API。我目前正在失眠和邮递员身上测试,我的问题是: 如果我提出一个请求,比如说HTTP://1.2.3.4:5000/controller,它就像一个符咒。但现在我要做的是通过一个类似的域进行访问 子域重定向到安装API的google云VM的IP地址(=>) 可能是与网络有关,但我不这么认为,因为当我删除C# 如何通过子域访问我的web api,c#,.net,asp.net-web-api,google-cloud-platform,C#,.net,Asp.net Web Api,Google Cloud Platform,我目前正在使用.NET做一个web API。我目前正在失眠和邮递员身上测试,我的问题是: 如果我提出一个请求,比如说HTTP://1.2.3.4:5000/controller,它就像一个符咒。但现在我要做的是通过一个类似的域进行访问 子域重定向到安装API的google云VM的IP地址(=>) 可能是与网络有关,但我不这么认为,因为当我删除[Authorize]部分时,我可以访问它 谢谢 编辑: 我只是想指出 我有,如果凭据正确,它会将一个令牌发送回我,然后我将该令牌用于所有操作,但这仅在我通
[Authorize]
部分时,我可以访问它
谢谢
编辑:
我只是想指出
我有,如果凭据正确,它会将一个令牌发送回我,然后我将该令牌用于所有操作,但这仅在我通过IP地址访问时有效,当我使用它发送给我的域时404未找到或未授权,甚至在我删除[授权]时匿名路由
part匿名路由开始工作
如前所述,如果我使用浏览器并将其重定向到如果您使用授权,则需要向web api传递令牌。由于您的通话在您注释[授权]时有效,因此您似乎:
未将令牌传递给控制器,或
您的令牌来自本地域
您的解决方案应该是调用登录例程,
获取令牌,并在您的邮差呼叫中使用该令牌。完成。问题是,当我重定向目标URL时,没有获得发送的头,因此授权:承载令牌
从未到达端点
为了解决这个问题,我必须在我的Google Cloud项目中配置一个DNS区域,在该区域中分配了VM,该域是唯一的,然后它将为您提供他们的DNS NS服务器URL
在此之后,我只需在域配置区域内添加这些DNS服务器URL,指向我的VM外部IP地址的新重定向,在这里,所有头都正确到达目的地,因为它们将直接到达您的主机。为了澄清,您是否真的使用某种HTTP“重定向”到IP地址,还是您正在使用DNS CNAME?@BryanLewis我正在使用DNS域,一位朋友借给我他的域名,我决定试一试。我尝试了可视重定向,其中域更改为目标ip,我还尝试了不可见重定向,其中域名保留。我的提供商是ovh.comHi这正是我的问题所在,如果凭据正确,它会将令牌发送回我,然后我会将该令牌用于所有操作,但这仅在我通过IP地址访问时有效,当我使用它发送给我的域未找到或未经授权时有效,甚至是匿名路由,当我删除授权部分时,匿名路由开始工作可能您的路由设置不正确?将[RoutePrefix(“/User”)]添加到UserController类声明的正上方,然后将[Route(“GetAll”)]置于GetAll()函数上方。从上述UserController类中删除[Route(“[controller]”)。然后您应该能够这样调用:它不起作用,但我开始认为这个问题远远超出了.NET代码,它可能与网络有关。我将去阅读一些关于重定向和DNS工作方式的文档,也许我遗漏了一些东西。谢谢
[Authorize]
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
private readonly IUserRepo _userRepo;
public UserController(IUserRepo userRepo)
{
Console.WriteLine("TEST2");
_userRepo = userRepo;
}
[HttpGet]
public List<User> GetAll()
{
return _userRepo.Get();
}
namespace sinba_backend
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddControllers();
var appSettingsSection = Configuration.GetSection(nameof(ConnSettings));
services.Configure<ConnSettings>(appSettingsSection);
services.AddSingleton<IConnSettings>(serviceProvider =>
serviceProvider.GetRequiredService<IOptions<ConnSettings>>().Value);
var appSettings = appSettingsSection.Get<ConnSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.SecretCredentials);
services.AddAuthentication(authOptions =>
{
authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(jsonWebToken =>
{
jsonWebToken.RequireHttpsMetadata = false;
jsonWebToken.SaveToken = true;
jsonWebToken.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false,
};
});
services.AddScoped<IUserRepo,UserRepo>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseCors(policy =>
policy.AllowAnyOrigin().
AllowAnyHeader().
AllowAnyMethod()
);
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}