Tag: asp.net core 2.0

为什么AntiForgeryTokenvalidation会失败?

我正在开发一个使用asp.net core2和Angular运行的web API应用程序。 详细的开发环境配置在这里 。 我正在尝试配置AntiForgeryTokenvalidation但它仍然失败。 我按照配置。 在这里 ,但我必须修改它,因为我的角度应用程序和asp.net服务器在两个不同的端口上运行,因为前端启动不会生成令牌。 我通过在应用程序组件ngOnInit中调用API路径( /api/Account/ContactInitialization )来启动后端,这允许我生成令牌。 配置如下所示, IServiceCollection服务: services.AddAntiforgery(options => { options.HeaderName = “X-CSRF-TOKEN”; options.SuppressXFrameOptionsHeader = false; }); 并在IApplicationBuilder Configure : app.Use(next => context => { string path = context.Request.Path.Value; if ( string.Equals(path, “/”, StringComparison.OrdinalIgnoreCase) || string.Equals(path, “/api/Account/ContactInitialization”, StringComparison.OrdinalIgnoreCase) || string.Equals(path, “/index.html”, StringComparison.OrdinalIgnoreCase)) { // We can send the […]

设置JsonConvert.DefaultSettings asp net core 2.0不能按预期工作

我在Startup.cs中有以下代码,并期望它覆盖默认的序列化选项。 我希望它覆盖我的asp net core 2.0项目中的每一个序列化,但是动作返回值不正确,我认为这个全局属性在core 2.0中不起作用 我把它写在app.UseMvc()之前的配置中; JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Formatting = Formatting.Indented, TypeNameHandling = TypeNameHandling.Objects, ContractResolver = new CamelCasePropertyNamesContractResolver(), Converters = new List { new StringEnumConverter() } };

找不到Microsoft.AspNetCore.Antiforgery

我正在将IIS.net核心2.0网站部署到IIS 10。 我确保我的应用程序在program.settings文件中使用了正确的ISS配置。 public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup() .Build(); } 在我的startup.cs文件中: public void ConfigureServices(IServiceCollection services) { services.Configure(options => { }); services.AddMvc(); } 然而,当我从命令行运行dotnet website.dll时,我收到命令行窗口中显示的以下错误消息: 未找到应用程序依赖项清单(website.deps.json)中指定的程序集:package:’Microsoft.AspNetCore.Antiforgery’,version:’2.0.1’path:’lib / netstandard2.0 / Microsoft.AspNetCore.Antiforgery .dll’当使用以下目标清单文件发布应用程序时,此程序集应位于本地运行时存储中:aspnetcore-store-2.0.3.xml 基于错误消息,我猜测Microsoft.AspNetCore.Antiforgery在我发布时没有捆绑,因为我在调试时没有收到此错误。 如何确保我的应用程序在发布到实时环境时能够找到Microsoft.AspNetCore.Antiforgery? 编辑:这是一个基本的.net核心网站。 除了使用IIS进行部署的上述更改之外,此时未对标准项目进行任何更改。 当我从IIS而不是命令行运行项目时,我收到502.5错误消息。

示例AJAX回调到ASP.NET Core Razor页面

我发现在页面上有多个处理程序的示例以及相关的命名约定(即OnPostXXX)和’asp-post-hanlder’标记帮助程序。 但是如何从AJAX调用中调用这些方法之一。 我有一个典型的MVC视图和控制器的旧示例,但这如何与Razor页面一起使用? 例如,如果我使用基本应用程序并将About.cshtml页面修改为以下内容: @page @model AboutModel @{ ViewData[“Title”] = “About”; } @ViewData[“Title”] @Model.Message @section Scripts { function ajaxTest() { console.log(“Entered method”); $.ajax({ type: “POST”, url: ‘/About’, // <– Where should this point? contentType: "application/json; charset=utf-8", dataType: "json", error: function (xhr, status, errorThrown) { var err = "Status: " + status + " " + […]

标记助手未在ASP.NET Core 2中处理

我添加了以下标记帮助器: using System; using System.Linq; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.TagHelpers; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Razor.TagHelpers; namespace X.TagHelpers { [HtmlTargetElement(Attributes = ValidationForAttributeName + “,” + ValidationErrorClassName)] public class ValidationClassTagHelper : TagHelper { private const string ValidationForAttributeName = “k-validation-for”; private const string ValidationErrorClassName = “k-error-class”; [HtmlAttributeName(ValidationForAttributeName)] public ModelExpression For { get; set; } [HtmlAttributeName(ValidationErrorClassName)] public string ValidationErrorClass […]

.net-core-2.0 azure app service 502.5错误

在CI部署到azure应用服务后获得502.5错误。 在调试控制台上运行dotnet {myproject}.dll时,这是我得到的错误: 未处理的exception:System.IO.FileLoadException:无法加载文件或程序集’System.Diagnostics.DiagnosticSource,Version = 4.0.2.1,Culture = neutral,PublicKeyToken = cc7b13ffcd2ddd51’。 定位的程序集的清单定义与程序集引用不匹配。 (来自HRESULT的exception:0x80131040)位于Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException和hostingStartupErrors)的Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()at My.IOEducation.Api.Program.Main(String [] args)in D:\ home \ site \ repository \ My.IOEducation.Api \ Program.cs:第11行 运行dotnet –version返回2.0.0 还有其他人遇到过这个以及如何解决的任何建议吗? 更新:这是项目文件的内容。 netcoreapp2.0

ASP.Net Core 2.0:无需请求即可创建UrlHelper

我正在为后台工作者创建一个UrlHelper来创建回调url,这意味着它不是正常请求的一部分,我可以通过DI请求它。 在ASP.Net 5中,我可以创建一个HttpRequest,并为它提供与构建我的应用程序相同的HttpConfiguration,但在ASP.Net Core 2.0中,UrlHelper依赖于完整的ActionContext,这有点难以制作。 我有一个工作原型,但它正在使用一个讨厌的黑客来从应用程序启动过程中走私路由数据。 有一个更好的方法吗? public class Capture { public IRouter Router { get; set; } } public static class Ext { // Step 1: Inject smuggler when building web host public static IWebHostBuilder SniffRouteData(this IWebHostBuilder builder) { return builder.ConfigureServices(svc => svc.AddSingleton()); } // Step 2: Swipe the route data in application startup […]

为HTTPS配置ASP.NET Core 2.0 Kestrel

TL; DR今天使用ASP.NET Core 2.0设置HTTPS的正确方法是什么? 我想将我的项目配置为使用https和他们在BUILD 2017中展示的证书。 我尝试了几个设置,但没有任何效果。 经过一番研究,我更加困惑。 似乎有很多方法来配置URL和端口……我已经通过代码看到了appsettings.json , appsettings.json ,在launchsettings.json我们也可以设置URL和端口。 有“标准”的方法吗? 这是我的appsettings.development.json { “Kestrel”: { “Endpoints”: { “Localhost”: { “Address”: “127.0.0.1”, “Port”: “40000” }, “LocalhostWithHttps”: { “Address”: “127.0.0.1”, “Port”: “40001”, “Certificate”: { “HTTPS”: { “Source”: “Store”, “StoreLocation”: “LocalMachine”, “StoreName”: “My”, “Subject”: “CN=localhost”, “AllowInvalid”: true } } } } } } 但是当我从命令行启动dotnet run或者从Visual Studio启动调试器时,它始终需要来自launchsettings.json的url和端口。 […]

ASP.Net Core 2.1中的身份:自定义AccountController

我已经安装了ASP.NET Core 2.1但是即使我使用带有Individual User Accounts ASP.NET Core 2.1创建了一个新的ASP.NET Core Web Application → Store user accounts in-app我找不到AccountController或Views。 我仍然可以注册和登录没有问题,但我找不到它的代码,它存在于2.0。

从Controller调用SignalR Core Hub方法

如何从Controller调用SignalR Core Hub方法? 我使用ASP.NET Core 2.0与Microsoft.AspNetCore.SignalR(1.0.0-alpha2-final)。 我有与Excel,SolidEdge通信的Windows服务…当操作完成后,它会在ASP.NET Core应用程序中向我的控制器发送请求。 现在我需要通过SignalR通知连接到服务器的所有客户端外部程序完成了一些任务。 我无法改变窗口服务的工作方式。 (无法从窗口服务连接到SignalR)。 我为旧的SignalR(GlobalHost.ConnectionManager.GetHubContext)找到了很多解决方案,但是已经发生了很多变化,这些解决方案不再适用了。 我的控制器: [Route(“API/vardesigncomm”)] public class VarDesignCommController : Controller { [HttpPut(“ProcessVarDesignCommResponse/{id}”)] public async Task ProcessVarDesignCommResponse(int id) { //call method TaskCompleted in Hub !!!! How? return new JsonResult(true); } } 我的中心: public class VarDesignHub : Hub { public async Task TaskCompleted(int id) { await Clients.All.InvokeAsync(“Completed”, id); […]