简体   繁体   English

无法使用不带 cookie 的不记名令牌将用户登录到 Azure AD

[英]Can't login user to Azure AD using a bearer token without cookies

Right now I'm hitting an API that uses Azure AD to authenticate users.现在我正在使用一个使用 Azure AD 对用户进行身份验证的 API。 Using a bearer token results in a HTTP 302 error redirecting users to login.使用不记名令牌会导致重定向用户登录的 HTTP 302 错误。 However, if I have a few cookies, the GET request goes through fine.但是,如果我有几个 cookie,则 GET 请求可以正常执行。

How come the bearer token is not acceptable on it's own, and needs cookies?为什么不记名令牌本身是不可接受的,并且需要 cookie? Is there a way around this?有没有解决的办法?

If you are using .net core and you want to use an access token to auth requests, pls make sure that you are using AzureADBearer as your Authentication service.如果您使用 .net core 并且想要使用访问令牌来验证请求,请确保您使用AzureADBearer作为您的身份验证服务。

Go to Startup.cs and try the code below :转到Startup.cs并尝试以下代码:

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.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace WebApplicationAzureAD
{
    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.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
                .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
            services.AddControllers();
        }

        // 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.UseHttpsRedirection();

            app.UseRouting();

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

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

Content of appsettings.json appsettings.json内容

{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "<your tenant domain>",
    "TenantId": "<your tenant ID>",
    "ClientId": "<your client ID>"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM