Blazor Server Webapi 不适用于邮递员

Posted

技术标签:

【中文标题】Blazor Server Webapi 不适用于邮递员【英文标题】:Blazor Server Webapi Not working on postman 【发布时间】:2021-02-10 07:20:10 【问题描述】:

我使用模板 Blazor Server、Webassembly、Shared 构建 Blazor。它工作正常。

但是当我尝试使用邮递员进行测试时。它总是显示这个

以下是我的测试控制器。我可以使用 blazor httpclient 访问休息。但我想用 postmna 测试

 using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SchoolHero.Server.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SchoolHero.Server.Controllers.V1

    [ApiVersion("1.0")]
    [AllowAnonymous]
    public class BuggyController : BaseApiController
    
        private readonly ApplicationDbContext _context;
        public BuggyController(ApplicationDbContext context)
        
            _context = context;
        

        [HttpGet("testauth")]
        [Authorize]
        public ActionResult<string> GetSecretStuff()
        
            return "super secret";
        

        [HttpGet("notfound")]
        public ActionResult GetNotFoundRequest()
        
            var thing = _context.Developers.Find(46);

            if (thing == null)
            
                return NotFound();
            

            return Ok();
        

        [HttpGet("servererror")]
        public ActionResult GetServerError()
        
            var thing = _context.Developers.Find(46);

            var thingToReturn = thing.ToString();

            return Ok();
        

        [HttpGet("badrequest")]
        public ActionResult GetBadRequest()
        
            return BadRequest();
        

        [HttpGet("badrequest/id")]
        public ActionResult GetNotFoundWithId(int id)
        
            return BadRequest();
        
    

/// 这是启动类。我注册。看起来不错

using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.ResponseCompression; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using SchoolHero.Server.Extensions; using System.Linq; using System.Threading.Tasks;

namespace SchoolHero.Server 
    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.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        
            services.AddContextInfrastructure(_configuration);
            services.AddEssentials();
            services.AddIdentityService(_configuration);
            services.AddControllersWithViews();
            services.AddRazorPages();

            services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
            
                builder.AllowAnyOrigin()
                       .AllowAnyMethod()
                       .AllowAnyHeader();
            ));
        

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        
            if (env.IsDevelopment())
            
                app.UseDeveloperExceptionPage();
                app.UseWebAssemblyDebugging();
            
            else
            
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            
            app.UseCors("CorsPolicy");
            app.ConfigureSwagger();
            app.UseHttpsRedirection();
            app.UseBlazorFrameworkFiles();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            
                endpoints.MapRazorPages();
                endpoints.MapControllers();
                endpoints.Map("api/**slug", HandleApiFallback);
                endpoints.MapFallbackToFile("index.html");
            );
        

        private Task HandleApiFallback(HttpContext context)
        
            context.Response.StatusCode = StatusCodes.Status404NotFound;
            return Task.FromResult(0);
        
     

【问题讨论】:

【参考方案1】:

根本没有代码? 我只能说,这看起来就像您正处于身份验证循环中。

【讨论】:

我在启动和 api 控制器上添加了一些代码 我没有配置到帐户/登录的路由 您没有指定身份验证:app.UseAuthentication();

以上是关于Blazor Server Webapi 不适用于邮递员的主要内容,如果未能解决你的问题,请参考以下文章

路径不适用于 Blazor 中允许的 IdentityServer CORS 端点,但适用于 Postman

Blazor PWA 适用于桌面浏览器,但不适用于智能手机

JWT 不记名令牌不适用于 ASP.Net Core 3.1 + Identity Server 4

WebAPI - 属性路由 POST 不适用于 WebAPI Cors?

Cors Policy 问题 Blazor WASM、Web API 和 Identity Server 4 和 IIS

通过 Angular 调用时,Windows 身份验证不适用于 WebAPI