.net核心中的身份验证

时间:2018-05-30 08:31:55

标签: .net angular asp.net-core frameworks

我有一个问题是在角度4中返回Windows身份验证。

我正在使用net Core,我收到以下错误:

  

状态代码 - 401未经授权

我在net Framework中尝试了相同的方法,但它正在运行。

我认为问题在于我想将数据发送到角度; save方法只调用一次,只使用请求方法:OPTIONS,而在net Framework中调用两次:OPTIONS => POST。

如何在网络核心中修复此问题?任何想法都会有所帮助。

保存方法:

namespace TestAuthorization.API.Controllers
{
    [Authorize]
    [Route("data")]
    public class DataController: Controller
    {
        [HttpPost]
        [Route("save")]
        public IActionResult Save(PostData data)
        {

            return Ok(data.ToString());
        }
    }
} 

启动课程:

namespace TestAuthorization.API
{
    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.AddSingleton<IConfiguration>(Configuration);
            services.AddMvc();
            services.AddCors();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();

            }
            app.UseCors(builder => builder.AllowAnyOrigin()
                                          .AllowAnyMethod()
                                          .AllowAnyHeader()
                                          .AllowCredentials());
            app.UseStaticFiles();
            app.UseDefaultFiles();
            app.UseMvc();
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Mvc did not find anything!");
            });
        }
    }
} 

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

您可能遇到预检请求问题。

我遇到了同样的问题。请看一下here。在那里,您将找到我如何解决预检请求的问题。