刚刚将一个 Web 项目升级到 .NET 6.0。项目已编译,但当我运行该网站时,出现以下错误:
InvalidOperationException:找不到包“System.Security.Cryptography”的编译库位置。
我正在使用 MiniProfiler.AspNetCore.Mvc 4.2.2。为了调试,我添加了 NuGet System.Security.Cryptography.Pkcs 包,但这并不能解决问题。
有什么解决问题的想法吗?
堆栈显示MiniProfilerMiddleware.cs正在引发异常:
InvalidOperationException: Cannot find compilation library location for package 'System.Security.Cryptography.Pkcs'
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List<string> assemblies)
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPartExtensions+<>c.<GetReferencePaths>b__0_0(CompilationLibrary library)
System.Linq.Enumerable+SelectManySingleSelectorIterator<TSource, TResult>.MoveNext()
System.Collections.Generic.List<T>.InsertRange(int index, IEnumerable<T> collection)
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetReferencePaths()
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetCompilationReferences()
System.Threading.LazyInitializer.EnsureInitializedCore<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
System.Threading.LazyInitializer.EnsureInitialized<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.get_CompilationReferences()
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.LazyMetadataReferenceFeature.get_References()
Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.ExecuteCore(RazorCodeDocument codeDocument)
Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.Execute(RazorCodeDocument codeDocument)
Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDocument document)
Microsoft.AspNetCore.Razor.Language.DefaultRazorProjectEngine.ProcessCore(RazorCodeDocument codeDocument)
Microsoft.AspNetCore.Razor.Language.RazorProjectEngine.Process(RazorProjectItem projectItem)
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(string relativePath)
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.OnCacheMiss(string normalizedPath)
Microsoft.AspNetCore.Mvc.Razor.Compilation.DefaultRazorPageFactoryProvider.CreateFactory(string …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc mvc-mini-profiler miniprofiler asp.net-core
我们已经建立了一个ASP.NET Core 2.1网站,其中的URL(例如www.example.org/uk和www.example.org/de)可以确定resx要显示的文件和内容。升级到ASP.NET Core 2.2后,页面会加载,但是生成的所有链接都会产生空白/空的href。
例如,以下链接:
<a asp-controller="Home" asp-action="Contact">@Res.ContactUs</a>
Run Code Online (Sandbox Code Playgroud)
将在2.2中产生一个空的href,如下所示:
<a href="">Contact us</a>
Run Code Online (Sandbox Code Playgroud)
但是在2.1中,我们得到了正确的href:
<a href="/uk/contact">Contact us</a>
Run Code Online (Sandbox Code Playgroud)
我们正在使用约束图来管理基于URL的语言功能-这是代码:
启动文件
// configure route options {lang}, e.g. /uk, /de, /es etc
services.Configure<RouteOptions>(options =>
{
options.LowercaseUrls = true;
options.AppendTrailingSlash = false;
options.ConstraintMap.Add("lang", typeof(LanguageRouteConstraint));
});
...
app.UseMvc(routes =>
{
routes.MapRoute(
name: "LocalizedDefault",
template: "{lang:lang}/{controller=Home}/{action=Index}/{id?}");
}
Run Code Online (Sandbox Code Playgroud)
LanguageRouteConstraint.cs
public class LanguageRouteConstraint : IRouteConstraint
{
private readonly AppLanguages _languageSettings;
public LanguageRouteConstraint(IHostingEnvironment hostingEnvironment)
{
var builder = new ConfigurationBuilder()
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: …Run Code Online (Sandbox Code Playgroud) 在ASP.NET中,我们有请求验证,但是在ASP.NET Core中,没有这样的事情。
我们如何最好地保护ASP.NET Core应用免受XSS的侵害?
请求验证消失了:https :
//nvisium.com/resources/blog/2017/08/08/dude-wheres-my-request-validation.html-
这个人推荐RegEx Models像这样:
[RegularExpression(@"^[a-zA-Z0-9 -']*$", ErrorMessage = "Invalid characters detected")]
public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)
...但这不适用于全球化/国际化,即æ,øå??等非拉丁字符。
X-XSS做>受限<XSS保护:https : //dotnetcoretutorials.com/2017/01/10/set-x-xss-protection-asp-net-core/像这样,但是只有有限的支持afaik:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.Use(async (context, next) =>
{
context.Response.Headers.Add("X-Xss-Protection", "1");
await next();
});
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
Microsoft的文档已有两年历史了:https : //docs.microsoft.com/zh-cn/aspnet/core/security/cross-site-scripting?view=aspnetcore-2.1并没有真正介绍它。
我正在考虑做一些简单的事情,例如:
myField = myField.Replace('<','').Replace('>','').Replace('&','').Repl...;
Run Code Online (Sandbox Code Playgroud)
我曾对Microsoft提出过同样的问题,但我很想听听人们如何在现实生活中解决此问题。
更新:我们正在努力实现的目标:
在我们的应用程序中,我们具有Web表单,人们可以在其中输入姓名,电子邮件,内容等。数据存储在数据库中,将来将在前端系统以及可能的其他系统(如RSS feed,JSON等)上查看。某些表单包含RTF编辑器(简洁),并允许用户标记其文本。恶意用户可以<script>alert('evil stuff');</script>在字段中输入。在ASP.NET Core到达数据库之前,先去除邪恶字符的最佳方法是什么-我宁愿完全不将邪恶脚本存储在数据库中。
我想像这样的东西可以工作:
const string RegExInvalidCharacters = @"[^&<>\""'/]*$";
[RegularExpression(RegExInvalidCharacters, ErrorMessage …Run Code Online (Sandbox Code Playgroud) 我正在使用这个autosuggest插件:http://aehlke.github.com/tag-it/
我从数据库中获取了一系列项目(现在只是一个普通的数组).该列表包括ID和标题.当我提交表单时,我想获得ID和标题.现在我只能得到Title.我想获得这两个值,以便可以创建新的引用(ID = 0),并且可以在不进行任何数据库查找的情况下插入现有的引用.
这是我的代码.
book.aspx的bookbehind - book.aspx.cs:
...
protected void btnSave_Click(object sender, EventArgs e)
{
Response.Write(txtReferences.Text); // this contains Titles, but I would like both values.
}
public class Reference
{
public string Title;
public int ID;
}
[WebMethod]
public static Array GetReferences(string title)
{
// this will be replaced by lookup in database.
List<Reference> References = new List<Reference>{
new Reference{ID=1, Title="My ref 1"},
new Reference{ID=2, Title="Ref ref 2"},
new Reference{ID=3, Title="Blah ref 3"},
new Reference{ID=0, …Run Code Online (Sandbox Code Playgroud) 我正在 ASP.NET Core MVC (2.1) 中的输入字段上进行自定义验证。我想添加一个简单的验证码字段,要求用户输入一些可以在 appsettings.json 文件中轻松重新配置的数字。我知道有很多图书馆都在做验证码,但这不是我想要的这种特殊情况。
我无法从 appsettings.json 获取值。下面的代码可以完美编译,但我不知道如何从 CaptchaCustomAttribute 类中的 appsettings.json 文件中获取值。
这是我的代码:
// appsettings.json
{
"GeneralConfiguration": {
"Captcha": "123456"
}
}
// GeneralConfiguration.cs
public class GeneralConfiguration
{
public string Captcha { get; set; }
}
// startup.cs / dependency injection
public void ConfigureServices(IServiceCollection services)
{
services.Configure<GeneralConfiguration>(Configuration.GetSection("GeneralConfiguration"));
}
// form model
public class ContactFormModel {
... simplified
[Display(Name = "Captcha")]
[Required(ErrorMessage = "Required")]
[CaptchaCustom]
public string Captcha { get; set; }
}
// CaptchaCustomAttribute.cs
public sealed class …Run Code Online (Sandbox Code Playgroud) 我们正在将 ASP.NET Core 2.2 项目升级到使用 EndPoint 路由的 ASP.NET 3.0。
我们有大量Url.RouteUrl使用命名路由构建的 url 列表,例如:
string url = Url.RouteUrl("blog-details", new { title = item.Title, id = item.Id });
// returns correct link of https://example.org/us/blog/some-title-6 in 2.2 but is blank in 3.0
[Route("~/{lang}/blog/{title}-{id}", Name= "blog-details")]
public async Task<IActionResult> Details(string title, int id)
{
}
Run Code Online (Sandbox Code Playgroud)
升级到 3.0 后,这些 url 只会产生一个空白的 href。我们startup.cs看起来像这样:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddControllersWithViews(options =>
{
options.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipeline)));
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
services.AddRazorPages();
...
}
Run Code Online (Sandbox Code Playgroud)
我们尝试用下面的替换,但这会创建错误的链接并且不允许我们作为变量重用,例如:
<a asp-action="Details" …Run Code Online (Sandbox Code Playgroud) 我不确定如何使用SQL从RSS-feed获取dc:creator的值.这是我的xml/rss-feed:
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>Foobar RSS</title>
<link>http://www.foobar.com/</link>
<description>RSS feed</description>
<language>en</language>
<ttl>15</ttl>
<item>
<title>This is my title</title>
<link>http://www.foobar.com/link/blabla</link>
<description>Bla..bla..bla..</description>
<dc:creator>John Doe</dc:creator>
<guid isPermaLink="false">00082EA751F1D905DE00E7CFA2417DA9</guid>
<pubDate>Wed, 26 Oct 2011 00:00:00 +0200</pubDate>
</item>
</channel>
</rss>
Run Code Online (Sandbox Code Playgroud)
在我的SQL中,我使用这样的东西来获取值 - 例如对于pubDate,我使用这样的东西:
DECLARE @xml XML
SET @xml = cast('my rss feed here' AS xml)
SELECT
convert(datetime,substring(T.nref.value('pubDate[1]','nvarchar(100)'),6,20)) as pubdate,
FROM @xml.nodes('//item') AS T(nref)
Run Code Online (Sandbox Code Playgroud)
这工作正常,但当我试图获得dc:creator值'John Doe'时,以下只是给出了一个错误:
SELECT
T.nref.value('dc:creator','nvarchar(100)') as creator
FROM @xml.nodes('//item') AS T(nref)
error:
XQuery [value()]: The name "dc" does not denote a namespace.
Run Code Online (Sandbox Code Playgroud)
我需要能够从rss-feed中选择多个列.任何人都可以提供解决方案或方向来获得dc:creator的价值吗?
我有另一个问题 - 如果你在子选择中这样做,你将如何构造代码? …
我的网址超过260个字符有问题
ASP.NET MVC 4.0Umbraco CMS Azure WebsitesIIS扼流并抛出以下错误:
错误请求 - 无效的网址
HTTP错误400.请求URL无效.
示例网址:
就我而言,URL长度为303个字符,没有任何查询字符串.将URL缩短为260个字符可以解决问题,但对我来说这不是一个可行的解决方案.由于我使用ASP.NET,我添加了以下内容web.config- 但是,问题仍然存在maxUrlLength="1024":
<?xml version="1.0"?>
<configuration>
<system.web>
<httpRuntime
requestValidationMode="4.0"
requestPathInvalidCharacters="<,>"
maxUrlLength="1024"
maxQueryStringLength="768"
relaxedUrlToFileSystemMapping="true" />
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
其他信息:我的所有网址都保持在2000-theh限制之下(参见不同浏览器中网址的最大长度是多少?)
有任何想法吗?
我想将多个值插入一行,但我想确保该行是唯一的,即没有重复的行。
我不确定如何做到这一点(如果只有一个值来检查即很容易,如下所示:SQL Server - 如何插入记录并确保它是唯一的)。
这是我的代码,但它不允许我插入唯一的行,因为它测试单列和多列组合。
CREATE TABLE myCities (
UserID int null,
CityID int null
)
DECLARE @UserID int, @CityID int
SET @UserID = 1
SET @CityID = 1
INSERT INTO myCities (UserID,CityID)
SELECT @UserID,@CityID
WHERE
@UserID NOT IN ( SELECT UserID FROM myCities WHERE UserID = @UserID )
AND
@CityID NOT IN ( SELECT CityID FROM myCities WHERE CityID = @CityID )
Run Code Online (Sandbox Code Playgroud) 我在 ASP.NET Core 2.1.1 项目中使用最新的 Rotativa.NetCore 程序集。NuGet ( https://www.nuget.org/packages/Rotativa.AspNetCore v. 1.0.6 ) 在部署 (win2016) 上不起作用,但在本地运行 (win10)。
IIS 在部署时给出 404,错误日志 (stdout) 显示:
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.Exception
at Rotativa.AspNetCore.WkhtmlDriver.Convert(String wkhtmlPath, String switches, String html, String wkhtmlExe)
at Rotativa.AspNetCore.WkhtmltopdfDriver.ConvertHtml(String wkhtmltopdfPath, String switches, String html)
at Rotativa.AspNetCore.ViewAsPdf.CallTheDriver(ActionContext context)
at Rotativa.AspNetCore.AsResultBase.BuildFile(ActionContext context)
at Rotativa.AspNetCore.AsResultBase.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at …Run Code Online (Sandbox Code Playgroud) 我正在对 ASP.NET Core 2.2 Razor View进行W3C 验证,但 W3C 给了我警告:
警告:来自命名空间http://www.w3.org/1999/xhtml 的元素 img 上的属性 alt 的值 不在 Unicode 规范化形式 C 中。
警告:来自命名空间http://www.w3.org/1999/xhtml 的元素 img 上的属性 title 的值 不在 Unicode 规范化形式 C 中。
我的数据存储在 MSSQL 数据库中nvarchar,其他所有内容都设置为 UTF-8。
控制器
using Dapper;
using System;
using System.Data;
using System.Data.SqlClient;
public class FileViewModel
{
public int FileId { get; set; }
public string Title { get; set; }
public string Source { get; set; }
}
private async …Run Code Online (Sandbox Code Playgroud) asp.net-core ×7
c# ×6
asp.net ×3
asp.net-mvc ×2
sql ×2
autosuggest ×1
azure ×1
jquery ×1
miniprofiler ×1
namespaces ×1
razor-pages ×1
rotativa ×1
rss ×1
tag-it ×1
unicode ×1
url ×1
xquery ×1
xss ×1