1

I have a problem to return windows authentication in angular 4.

I'm using net Core and I get the following error:

status code - 401 Unauthorized

I tried the same method in net Framework and it is working.

I think the problem is when I want to send data to angular; the save method is called once, only with the request method: OPTIONS, while in net Framework is called twice: OPTIONS => POST.

How I can fix this problem in net Core? Any ideas will be helpful.

Save method :

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

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

Startup class:

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

user2004
  • 1,783
  • 4
  • 15
  • 42

1 Answers1

0

You probably have issues with preflight requests.

I had the same problem. Please take a look here. There you will find how I resolved issue with preflight requests.

Joe Belladonna
  • 1,349
  • 14
  • 20
  • Thanks for the answer. I need it for windows authentication, the OWIN is working? If it works please can give an answer how to configure it, or where I can found how to do it? – user2004 May 30 '18 at 10:31
  • @Teodora Malec I have used that for windows with IIS. Take a look at the second post here: https://stackoverflow.com/questions/48813793/angular5-webapi-token-authorization-not-working Follow links there and you will find out how to setup it. If you need more help let me know. – Joe Belladonna May 30 '18 at 10:33
  • I haven't generated App-Start/Startup.Auth.cs because I'm using Windows Auth and it is generated by individual user account. How cand I generate this file or something that would help me? – user2004 May 31 '18 at 05:53
  • @Teodora Malec Is your route correct. Maybe instead of [Route("data")] you should have [Route("api/data")]. Check your routes. – Joe Belladonna May 31 '18 at 06:18