我有一个使用 Axios 调用 .NET 6 Web API 的 React 应用程序。
在program.cs文件中,我添加了builder.Services.AddCors和app.UseCors,如下所示。
但我仍然收到 CORS 错误和 404 预检。
该方法适用于 .NET 5 Web Api。
我们需要为 .NET 6 Web Api 设置什么吗?
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
<removed>
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors();
// Add services to the container.
<removed>
// App settings
<removed>
<removed>
builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
});
// AutoMapper
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
<removed>
// Firebase
<removed>
var app = builder.Build();
Run Code Online (Sandbox Code Playgroud)
CORS …
当我运行一个全新的 Web 应用程序时,我收到以下错误消息:
System.MissingMethodException: '找不到方法:'Void Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions.set_AllowAreas(Boolean)'。'
我猜我需要下载一个 nuget 包,但不知道是哪一个?
我的 startup.cs 代码如下 - 故障发生在 endpoints.MapRazor();
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Web
{
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 …Run Code Online (Sandbox Code Playgroud) 我在设置 Active Directory 过滤器来同步包含所有用户的 MySQL 数据库时遇到问题。我无法创建一个仅检索更新日期大于给定日期的用户的过滤器。
我尝试在过滤器上使用 uSNChanged 属性,但它返回 0 结果。
有什么建议欢迎留言,谢谢大家
我无法在ASP .NET Core中实现基于策略的授权,以执行剃须刀页面上的操作。
我通读了有关授权的详尽文档,并以其示例为指导。
剃刀页面动作代码:
[Authorize(Policy = "test")]
public async Task<IActionResult> OnGetCreateAsync(string id)
Run Code Online (Sandbox Code Playgroud)
服务配置中的代码:
_ = services.AddAuthorization(options => {
options.AddPolicy("test", policy =>
policy.RequireAssertion(context =>
false));
});
Run Code Online (Sandbox Code Playgroud)
我希望如果我调用动作或端点服务,例如
GET /Account?handler=Create
Run Code Online (Sandbox Code Playgroud)
那么该请求将以403状态响应被拒绝,因为“测试”策略指出每个人都是未经授权的。但是,在实际中,该操作已成功调用。
我正在尝试使用 SQL Server Express 和 EF Core 3.1.3 来衡量 ASP.NET Core 3.1 中异步与同步的性能,并且有两个完全相同的函数,除了一个是异步的,一个是同步的:
[HttpGet("search/description/{searchString}")]
public async Task<ActionResult<IEnumerable<Products>>> SearchForProductsDescription(String searchString) {
return await _context.Products.Where(p => p.Description == searchString).ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)
以及同步版本:
return _context.Products.Where(p => p.Description == searchString).ToList();
Run Code Online (Sandbox Code Playgroud)
我使用 jmeter 作为基准工具,同步函数比异步函数更快(正如预期的那样),但是当我增加 jmeter 中的线程数以使平均响应时间为 500ms< 时,同步代码仍然更快。我尝试过在数据库中使用 1000 行和 20000 行,但它仍然更快。我试图找到一种场景,其中异步函数比同步函数更快,但我遇到了麻烦,是否有什么我出错或误解的地方?
我想在服务器上运行一些 xUnit 测试,而不必在那里安装 .NET Core SDK。
服务器是安装了.NET Core运行时v2.2的Windows服务器。
我该怎么办呢?
我使用 Windows 身份验证创建了一个服务器端 Blazor 应用程序。它创建了以下文件。
登录Display.razor
<AuthorizeView>
Hello, @context.User.Identity.Name!
</AuthorizeView>
Run Code Online (Sandbox Code Playgroud)
但是,它显示“域\用户名”。它是一种显示用户名字的方法吗?
我试图打印所有的索赔类型。
@foreach(var c in context.User.Claims)
{
<p>@c.Value @c.Type</p>
}
Run Code Online (Sandbox Code Playgroud)
但是,只有一种类型带有名称。(价值为DOMAIN\username)
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name
Run Code Online (Sandbox Code Playgroud) active-directory asp.net-authorization razor asp.net-core blazor
我从PowerShell运行以下命令:
Get-ADUser -Server ad.com -Filter '*' -Properties '*' Export-Csv 'C:\Users\myFolder\file.csv' -NoTypeInformation -Delimiter '|'
Run Code Online (Sandbox Code Playgroud)
只是尝试识别我可以用作'主键'的属性,例如EmployeeID或EmployeeNumber.AD属性列表:[link] AD是否提供主键?
我已成功将项目更新为netcoreapp2.2。我可以还原nuget程序包并构建项目,而不会出现任何问题。我使用CD / CI,并且在构建服务器上,还原nuget时收到错误消息。这些来自日志:
2018-12-14T12:35:33.4556576Z ## [错误] nuget命令失败,出现退出代码(1)和错误(NU1607:为Microsoft.AspNetCore.Authentication.Abstractions检测到版本冲突。直接从项目中引用包解决此问题PM.Service.API(> = 1.0.0)-> Microsoft.AspNetCore.Mvc.Formatters.Xml(> = 2.2.0)-> Microsoft.AspNetCore.Mvc.Core(> = 2.2.0) -> Microsoft.AspNetCore.Authorization.Policy(> = 2.2.0)-> Microsoft.AspNetCore.Authentication.Abstractions(> = 2.2.0)PM.Service.API(> = 1.0.0)-> Microsoft.AspNetCore App(> = 2.1.0)-> Microsoft.AspNetCore.Authentication.Abstractions(> = 2.1.0)。NU1607:为Microsoft.AspNetCore.Hosting检测到版本冲突。直接从项目中引用该包即可解决此问题。 .Service.API(> = 1.0.0)->Microsoft.AspNetCore.Hosting.WindowsServices(> = 2.2.0)-> Microsoft.AspNetCore.Hosting(> = 2.2.0)TM.Service.API(> = 1.0.0)-> Microsoft.AspNetCore.App(> = 2.1.0)-> Microsoft.AspNetCore.Hosting(> = 2.1.0)。
在我的项目中,我有以下软件包参考:
我该如何解决这个问题?谢谢
我的preveus问题中的这个主题:如何在Windows事件查看器中删除和创建日志
我创建了wpf app.我用3种方式捕捉未处理的异常:
public partial class App : Application
{
public App()
{
DispatcherUnhandledException += App_DispatcherUnhandledException;
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
}
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我正在创建这样的异常:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
throw new Exception("Test exception");
}
}
Run Code Online (Sandbox Code Playgroud)
在执行方法(Dispatcher_UnhandledException,)之后CurrentDomain_UnhandledException,App_DispatcherUnhandledException这个异常仍在抛出.Windows事件查看器正在创建这样的日志
描述:由于未处理的异常,进程终止.异常信息:System.Data.ProviderBase.DbConnectionFactory.TryGetConnection的System.InvalidOperationException(System.Data.Common.DbConnection,System.Threading.Tasks.TaskCompletionSource
1<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions, System.Data.ProviderBase.DbConnectionInternal, System.Data.ProviderBase.DbConnectionInternal ByRef) …
我有这段代码,我想找到一种方法来尽可能地清理它。希望人们提出以下建议:
if (App.viewablePhrases.Count() == 1)
{
await SetMessageAsync(
msgType: "Practice",
msg1: "Practice Mode",
msg2: $"You have 1 card viewable out of {selCds} {msg}\n\nThe card in your deck has a total of {selPts} {ptsMsg1} {ptsMsg2}",
btnText: App.cardsViewed ? "Continue Practice" : "Start Practice ");
return true;
}
if (App.viewablePhrases.Count() > 1)
{
await SetMessageAsync(
msgType: "Practice",
msg1: "Practice Mode",
msg2: "You have " + App.viewablePhrases.Count() + " cards viewable out of a total of " + selCds + msg +
"\n\nThe …Run Code Online (Sandbox Code Playgroud) c# ×7
asp.net-core ×5
ldap ×2
.net-6.0 ×1
.net-core ×1
asynchronous ×1
benchmarking ×1
blazor ×1
nuget ×1
php ×1
powershell ×1
razor ×1
razor-pages ×1
unit-testing ×1
webapi ×1
windows ×1
wpf ×1
xunit ×1