Web 应用程序应允许拥有 AD 帐户的内部员工使用 Azure AD 身份验证在应用程序中进行身份验证。外部用户应该能够使用 ASP.NET Core Identity 注册和登录。我可以单独实现每一个,但不能在同一个应用程序中一起实现。当我将两种身份验证添加到同一个应用程序时,ASP.NET Core Identity 可以完美运行。我可以毫无问题地使用 Identity 注册和登录。但是,当我尝试使用 Azure AD 登录时,应用程序将我重定向到租户的登录页面,我提交了用户名和密码,它将我重定向回应用程序,但没有用户通过身份验证。我再次点击登录按钮,同样的事情发生了。似乎网络应用程序或浏览器没有保存访问令牌或类似的东西。
我究竟做错了什么?甚至可以在同一个应用程序上进行两组身份验证吗?
谢谢。这是代码:
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
Run Code Online (Sandbox Code Playgroud)
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration) => Configuration = configuration;
public void ConfigureServices(IServiceCollection services)
{
// Add Azure AD authentication
services.AddAuthentication(defaultScheme: AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
// Add the application db context
services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
// Add Identity using …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 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) …Run Code Online (Sandbox Code Playgroud) 在 ASP.NET Core API 项目中,我需要验证位于与 Authorization 标头不同的标头中的另一个 JWT Bearer 令牌。例如,假设发送一个 GET 请求来获取产品,/api/products并在名为 的标头中包含承载令牌AccessToken。
curl --location --request GET 'https://localhost/api/products' \
--header 'AccessToken: <bearer_token>'
Run Code Online (Sandbox Code Playgroud)
我引用Microsoft.AspNetCore.Authentication.JwtBearer包并在 API 项目中设置身份验证,如下所示:
curl --location --request GET 'https://localhost/api/products' \
--header 'AccessToken: <bearer_token>'
Run Code Online (Sandbox Code Playgroud)
但是,我在JwtBearerOptions Class中找不到有关标头名称的任何内容。
如何配置 JWT 身份验证以从名为“AccessToken”的标头读取 JWT? 是否可以使用 Microsoft.AspNetCore.Authentication.JwtBearer 包?
我们可以使用.NET 7中的新INumber<TSelf>接口来引用任何数字类型,如下所示:
using System.Numerics;
INumber<int> myNumber = 72;
INumber<float> mySecondNumber = 93.63f;
Run Code Online (Sandbox Code Playgroud)
但是,由于 中的类型限制INumber,我们无法拥有可以保存任何数字类型的通用引用。以下代码无效:
using System.Numerics;
INumber myNumber = 72;
myNumber = 93.63f;
Run Code Online (Sandbox Code Playgroud)
我怎样才能拥有任何数字对象的数组并调用需要对象的方法INumber<TSelf>。
using System.Numerics;
object[] numbers = new object[] { 1, 2.5, 5, 0x1001, 72 };
for (int i = 0; i < numbers.Length - 1; i++)
{
Console.WriteLine("{0} plus {1} equals {2}", numbers[i], numbers[i + 1], AddNumbers(numbers[i], numbers[i + 1]));
}
static T AddNumbers<T>(T left, T right) where T : …Run Code Online (Sandbox Code Playgroud) 我正在尝试执行包含 WHERE 作为异步函数的查询。就像使用该FirstAsync操作一样,但没有,WhereAsync所以我想知道是否有一些解决方法。
我有一个ApplicationRepository具有功能的对象GetEntitiesAsync,我尝试了以下方法:
public async Task<IEnumerable<TEntity>> GetEntitiesAsync<TEntity>(Func<TEntity, bool> selector) where TEntity : class =>
await _context.Set<TEntity>().Where(selector).AsQueryable().ToArrayAsync();
Run Code Online (Sandbox Code Playgroud)
然而,这行代码会抛出异常:
System.InvalidOperationException: The source IQueryable doesn't implement IAsyncEnumerable<OneStopApp.Models.CustomForm>. Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations.
Run Code Online (Sandbox Code Playgroud) c# ×5
asp.net-core ×3
.net ×2
azure ×2
.net-7.0 ×1
asp.net-core-authenticationhandler ×1
jwt ×1