【c# – ASP.NET Core MVC中使用Prometheus的示例项目?】教程文章相关的互联网学习教程文章

c# – 使用Blazor将JWT存储在ASP.NET Core中【代码】

我按照本教程:https://medium.com/@st.mas29/microsoft-blazor-web-api-with-jwt-authentication-part-1-f33a44abab9d 我下载了例子:https://github.com/StuwiiDev/DotnetCoreJwtAuthentication/tree/Part2 我可以看到令牌已创建,但我不明白它是如何保存或应该保存在客户端,因为每次访问具有Authorize标记的SampleDataController时,它都会返回401. 使用Postman调用和添加令牌时,它可以正常工作. 我对用户进行身份验证缺少什么? ...

c# – ASP.NET Core MVC(以前的MVC 6)Razor错误没有被异常过滤器捕获【代码】

背景:使用Dapper和Repository Pattern的ASP.NET 5(ASP.NET Core 1.0)MVC 6应用程序 显然,与其他所有网站/应用程序一样,我正在尝试消除我网站中弹出的大多数/所有异常. 我实现了一个ExceptionFilter,以便捕获所有未处理的异常,如下所示:public class UnhandledExceptionFilter : ActionFilterAttribute, IExceptionFilter {private readonly IErrorRepo _errorRepo;public UnhandledExceptionFilter(IErrorRepo errorRepo){_error...

c# – ASP.NET Core:从解决方案中的另一个项目访问appsettings【代码】

在我的Startup.cs类中,我有以下配置构建,它初始化db上下文:var builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile("appsettings.json", true, true).AddJsonFile($"appsettings.{env.EnvironmentName}.json",true).AddEnvironmentVariables();Configuration = builder.Build();NHibernateUnitOfWork.Init(Configuration["ConnectionStrings:OracleConnection"]);NHibernateUnitOfWork作为类库项...

c# – 我是否需要在ASP.NET Core中锁定单例?【代码】

这是我的代码:public class RouteSingleton {private IDictionary<string, string> _dealCatLinks;private IDictionary<string, string> _sectionLinks;private IDictionary<string, string> _categoryLinks;private IDictionary<string, string> _materials;private IDictionary<string, string> _vendors;public RouteSingleton(IDealService dealService, ICategoryService categoryService, IVendorService vendorService){th...

c# – ASP.NET Core 2.0 AzureAD身份验证无法正常工作【代码】

我有一个ASP.NET Core 2.0应用程序设置,我想使用AzureAd进行我公司目录的身份验证.我已经设置了类和启动方法并使身份验证工作正常,我遇到的问题是我正在尝试设置OnAuthorizationCodeReceived事件的事件处理程序,以便我可以请求一个用户令牌用于Microsoft图形调用. 在我的Startup.cs中,我有以下代码public void ConfigureServices(IServiceCollection services){services.AddAuthentication(sharedOptions =>{sharedOptions.Default...

c# – 带有Angular 6捆绑文件的项目模板的ASP.NET Core 404(错误的dist输出目录)【代码】

我使用Angular项目模板https://docs.microsoft.com/en-US/aspnet/core/client-side/spa/angular?view=aspnetcore-2.1&tabs=visual-studio创建了一个ASP.NET Core应用程序 生成输出 在IIS上部署时,基本href是< base href =“/”> 生成的dist / index.html中脚本的路径是:<script type="text/javascript" src="runtime.js"></script> <script type="text/javascript" src="polyfills.js"></script> <script type="text/javascript" ...

c# – 使用cookie进行ASP.NET Core .NET Framework测试【代码】

我创建了一个会话中间件,想要测试它.所以我使用TestServer进行测试. 测试代码如下:using System.Linq; using System.Threading.Tasks; using ComponentsTest.StartUps; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; using NUnit.Framework;namespace ComponentsTest.IntegrationTest {[TestFixture]public class SessionMwTest{[SetUp]public void Setup(){_server = new TestServer(_hostBuilder)...

c# – 为什么ASP.NET Core Identity 2.0授权过滤器导致我获得404?【代码】

我有一个控制器,我想只限制一个特定的角色,让我们说管理员.在使用admin角色设置用户之后,我可以使用IsInRoleAsync方法(返回true)验证他是否在该角色上.使用[Authorize(Roles =“admin”)]设置属性时,我得到的是同一个用户的404.我正在使用持有者令牌(我不认为这是相关的,但无论如何),这是我尝试调试所做的: 控制器没有[授权]:返回资源. [好] 带[授权]的控制器:仅当我使用身份验证时才返回资源:承载[访问令牌] [确定] 带[Author...

c# – ASP.NET Core DI构造函数与RequestServices [复制]【代码】

参见英文答案 > What’s the difference between the Dependency Injection and Service Locator patterns? 13个> Is ServiceLocator an anti-pattern? 7个为什么通过HttpContext.RequestServices或IServiceProvider请求服务会考虑不好的做法.我可以在这个地方读到这句话:It is recommended to use constructor injection instead of getting ...

c# – 无法解析ILogger Simple Injector ASP.NET Core 2.0【代码】

我们在ASP.NET Core应用程序中使用Simple Injector.最近我们决定使用Serilog进行日志记录. 配置在Program.cs中完成,如documentation中所述.然后,为了使Simple Injector能够解析ILoggerFactory,我做了类似这样的事情:public void Configure(IApplicationBuilder app, IHostingEnvironment env) {if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseMvc();// As per my understanding, we force SimpleInjector to ...

c# – ASP.NET Core在Web API中处理自定义响应/输出格式的方法【代码】

我想创建自定义JSON格式,它会将响应包装在数据中并返回Content-Type之类的vnd.myapi+json目前我创建的类似于我在控制器中返回的包装类,但如果可以在引擎盖下处理它会更好:public class ApiResult<TValue> {[JsonProperty("data")]public TValue Value { get; set; }[JsonExtensionData]public Dictionary<string, object> Metadata { get; } = new Dictionary<string, object>();public ApiResult(TValue value){Value = value;} ...

c# – ASP.NET Core 2.1 Routing – CreatedAtRoute – 没有路由匹配提供的值【代码】

我有以下情况[Route("api/[controller]")][ApiController]public class FooController: ControllerBase{[HttpGet("{id}", Name = "GetFoo")]public ActionResult<FooBindModel> Get([FromRoute]Guid id){...}}[Route("api/[controller]")][ApiController]public class Foo2Controller: ControllerBase{[HttpPost("/api/Foo2/Create")]public ActionResult<GetFooBindModel> Create([FromBody]PostFooBindModel postBindModel){...r...

c# – 仅访问模型时的ASP.NET Core NullReferenceException【代码】

我在尝试使用强类型模型创建视图时遇到问题.无论我作为模型传递给View(),我总是在访问模型时收到NullReferenceException. 在执行剃刀页面的其余部分之前,我甚至无法检查模型是否为null;简单地做一个if(Model!= null)也会抛出相同的NullReferenceException. Index.cshtml@page @model EncodeModel @{Layout = "~/Pages/Shared/_Layout.cshtml"; }<h2>Encode</h2><div id="progress">@await Html.PartialAsync("~/Encoder/MVC/Encod...

c# – 如何在asp.net core web api中绑定Json Query字符串【代码】

asp.net web API中的以下代码工作正常,但在Asp.net核心中不起作用. 端点api / devices?query = {“deviceName”:“example”}[HttpGet] public Device ([FromUri] string deviceName) { var device = context.Computers.Where(x => x.deviceName == deviceName);return device; }[FromUri]属性不存在asp.net核心web API,我尝试使用以下,但没有成功.[HttpGet] public Device Get([FromQuery] string deviceName) {return...

c# – 没有Identity ASP.NET Core v2的Cookie中间件【代码】

我试图在不使用身份的情况下进行身份验证.我发现了一些文章描述了如何在其他版本中执行它,但对于ASP.NET Core 2没有任何内容. 以下是我拼凑在一起的内容.但是当它到达SignInAsync时会抛出异常InvalidOperationException:没有配置身份验证处理程序来处理该方案:MyCookieMiddlewareInstance// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceC...