【发布时间】:2020-05-27 10:08:56
【问题描述】:
我正在尝试使用 Azure Active Directory 处理 Web 应用程序的身份验证。但是,当我尝试使用AuthorizeAttribute 执行操作时,应用程序会抛出OptionsValidationException。出现以下错误:
Microsoft.Extensions.Options.OptionsValidationException: The 'Instance' option must be provided.
at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
at Microsoft.Extensions.Options.OptionsMonitor`1.<>c__DisplayClass11_0.<Get>b__0()
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy`1.CreateValue()
at System.Lazy`1.get_Value()
at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions)
at Microsoft.Extensions.Options.OptionsMonitor`1.Get(String name)
at Microsoft.AspNetCore.Authentication.AzureAD.UI.AzureADOpenIdConnectOptionsConfiguration.Configure(String name, OpenIdConnectOptions options)
at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
at Microsoft.Extensions.Options.OptionsMonitor`1.<>c__DisplayClass11_0.<Get>b__0()
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy`1.CreateValue()
at System.Lazy`1.get_Value()
at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd(String name, Func`1 createOptions)
at Microsoft.Extensions.Options.OptionsMonitor`1.Get(String name)
at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)
at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.ChallengeAsync(AuthenticationProperties properties)
at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
我不知道是什么原因造成的。代码如下:
添加对Microsoft.AspNetCore.Authentication.AzureAD.UI 3.1.1 版的包引用。
启动类
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(defaultScheme: AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options =>
{
options.ClientId = "<client_id_goes_here>";
options.TenantId = "<tenant_id_goes_here>";
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
家庭控制器
仅使用一个控制器。
public class HomeController : Controller
{
[Route("")]
[AllowAnonymous]
public string Index() => "Hello Anonymous User!";
[Route("restricted")]
[Authorize]
public string Restricted() => $"Hello, {User.Identity.Name}.";
}
当您运行应用并点击 Index 操作时,您会得到异常输出:
Hello Anonymous User!
当您点击/restricted 端点时,就会引发异常。
【问题讨论】:
-
您是否尝试在您的
configure方法中添加app.UseAuthentication()- 就在app.UseAuthorization()之前? -
@SimplyGed 是的,结果相同。
标签: c# .net azure asp.net-core