简体   繁体   English

Asp.Net核心挑战返回Null URI异常错误

[英]Asp.Net Core Challenge returns Null URI Exception error

I am creating an ASP.NET core web API and want to integrate microsoft Azure AD authentication services. 我正在创建一个ASP.NET核心Web API,并希望集成Microsoft Azure AD身份验证服务。 On compile-time everything works just fine. 在编译时,一切正常。 However, when I access the route meant to return a Challenge, I get a null URI Exception (Stacktrace below). 但是,当我访问要返回Challenge的路由时,我得到一个空URI异常(下面的Stacktrace)。 I have tried specifying a challenge URI in my ConfigureServices: 我已经尝试在我的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();

        }

I have also tried changing the redirect URI in my route: 我也尝试在路由中更改重定向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);
        }


    }
}

I cant seem to find the root of the issue, and the stacktrace makes no sense to me: 我似乎无法找到问题的根源,而堆栈跟踪对我来说毫无意义:

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()

I am using ASP.NET Core 2.1.1 with MVC, Cors, Azure AD, and EntityFramework There is no in-project CSHTML, as we are using a separate front end (Using VueJS) 我使用ASP.NET Core 2.1.1与MVC,Cors,Azure AD和EntityFramework没有项目内CSHTML,因为我们使用单独的前端(使用VueJS)

I think the problem is because the Instance property is not set on your AzureADOptions 我认为问题是因为您的AzureADOptions上未设置Instance属性

See this link 看到这个链接

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

So add, for example 例如,添加

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

In all likelihood you need to set the URI. 您很可能需要设置URI。

There are three things I suggest you check: 我建议你检查三件事:

  1. Have you added your database endpoint and key to the config file? 您是否已将数据库端点和密钥添加到配置文件中? It looks like the issue might be with your connection strings. 看起来问题可能出在您的连接字符串上。 Add the keys within the app settings of your config file if you have not done so already. 如果您还没有在配置文件的应用程序设置中添加密钥。 Ensure that your app settings are there and any web service URLs are being referenced properly. 确保您的应用设置存在,并且正确引用了任何Web服务URL。
  2. Do a null check on the URI to ensure that a null value does not get passed in. 对URI执行空检查以确保不传入空值。
  3. Check the system logs on your app server. 检查应用服务器上的系统日志。
  4. Ensure that you are passing in all necessary parameters. 确保您传递了所有必要的参数。
  5. Register the app to a new application pool. 将应用程序注册到新的应用程序池。

Hope this helps! 希望这可以帮助!

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

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