c# - Asp.Net Core Challenge 返回空 URI 异常错误

标签 c# asp.net-web-api azure-active-directory

我正在创建一个 ASP.NET 核心 Web API,并希望集成 Microsoft Azure AD 身份验证服务。在编译时一切正常。但是,当我访问旨在返回挑战的路由时,我得到一个空 URI 异常(下面的 Stacktrace)。我已尝试在我的 ConfigureServices 中指定挑战 URI:

 public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<AppContext>(options => options.UseSqlite("Data Source=app.db"));

            // Identity/Authorization Services
            services.AddAuthentication(sharedOptions =>
                {
                    sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                })
                .AddAzureAD(options =>
                {
                    options.ClientId = Configuration["Authentication:Microsoft:ApplicationId"];
                    options.ClientSecret = Configuration["Authentication:Microsoft:Password"];
                    options.SignedOutCallbackPath = "/api/v1/accounts/signout";
                })
                .AddCookie(options => options.AccessDeniedPath = "/api/v1/accounts/accessdenied");

            services.AddTransient<IRecognitionService, RecognitionService>();
            services.AddTransient<IProfileService, ProfileService>();


            services.AddMvc();

        }

我也尝试过更改我的路由中的重定向 URI:

    [Route("api/v1/accounts/[action]")]
    public class AccountController : Controller
    {
        public async Task<IActionResult> AccessDenied()
        {
            return Unauthorized();
        }

        [HttpGet]
        public async Task<IActionResult> Signin()
        {
            return Challenge(new AuthenticationProperties {RedirectUri = "/accessdenied"},
                AzureADDefaults.AuthenticationScheme);
        }


    }
}

我似乎找不到问题的根源,堆栈跟踪对我来说毫无意义:

System.ArgumentNullException: Value cannot be null.
Parameter name: uriString
   at System.Uri..ctor(String uriString)
   at Microsoft.AspNetCore.Authentication.AzureAD.UI.OpenIdConnectOptionsConfiguration.Configure(String name, OpenIdConnectOptions options)
   at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.<InitializeAsync>d__42.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.<GetHandlerAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationService.<ChallengeAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.<ChallengeAsync>d__54.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationService.<ChallengeAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.ChallengeResult.<ExecuteResultAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeResultAsync>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResultFilterAsync>d__28`2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeResultFilters>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilter>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()

我将 ASP.NET Core 2.1.1 与 MVC、Cors、Azure AD 和 EntityFramework 结合使用 没有项目内的 CSHTML,因为我们使用的是单独的前端(使用 VueJS)

最佳答案

我认为问题是因为您的 AzureADOptions 上未设置 Instance 属性

查看此链接

https://github.com/aspnet/AADIntegration/issues/34

所以添加,例如

.AddAzureAD(options => 
{
  options.Instance = "https://login.microsoftonline.com"
}

关于c# - Asp.Net Core Challenge 返回空 URI 异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51250560/

相关文章:

azure - 无法从 AAD 获取不记名 token 来使用 Web API

azure-active-directory - 为什么 Microsoft Graph 筛选器在图形浏览器中有效,但在 API 中无效

c# - 如何将当前运行的程序集复制到 Windows 启动项

c# - 我是否需要为我的 powerpoint 加载项安装 Microsoft Visual Studio 2010 Tools for Office Runtime(x86 和 x64)?

c# - 在 Windows 资源管理器中检测文件选择

Azure 中的 API 安全最佳实践

javascript - 动态 CRM : JavaScript GET request is not retrieving records using Web. API

c# - 如何从表达式属性中获取名称?

asp.net-mvc-4 - ASP.Net Web Api 未在 POST 上绑定(bind)模型

azure-active-directory - 尝试更新 'mobilePhone' 时 Microsoft Graph API 权限不足