我正在尝试创建一个输出原始html的Angular2自定义管道.我希望它只是将输入中的换行符转换为HTML换行符.如何从angular2管道输出原始HTML?
以下是我当前的管道定义,但它转义了我不想要的HTML:
import {Pipe, PipeTransform} from '@angular/core';
/*
* Converts newlines into html breaks
*/
@Pipe({ name: 'newline' })
export class NewlinePipe implements PipeTransform {
transform(value: string, args: string[]): any {
return value.replace(/(?:\r\n|\r|\n)/g, '<br />');
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:只是一个快速的说明,因为这个问题似乎获得了许多观点和活动,我将留下我接受的答案,即使对于我的具体例子,用例css可能是一个更好的选择,事实上我改变了什么.但我的实际问题是"如何从角度2管道输出原始html",而不是"什么是渲染换行符的最佳方式",这是接受的答案所显示的.
但请注意,如下面的评论中所述,如果您在接受的答案中使用该方法,则需要确保您不会呈现未经检查的用户输入,因为这可能会导致您遇到XSS漏洞.
我正在使用Swashbuckle为webapi2项目生成swagger文档\ UI.我们的模型与一些传统接口共享,因此我想在模型上忽略一些属性.我不能使用JsonIgnore属性,因为旧版接口也需要序列化为JSON,所以我不想全局忽略这些属性,只是在Swashbuckle配置中.
我在这里找到了一种记录方法:
https://github.com/domaindrivendev/Swashbuckle/issues/73
但这似乎与目前的Swashbuckle版本已经过时了.
为旧版Swashbuckle推荐的方法是使用IModelFilter实现,如下所示:
public class OmitIgnoredProperties : IModelFilter
{
public void Apply(DataType model, DataTypeRegistry dataTypeRegistry, Type type)
{
var ignoredProperties = … // use reflection to find any properties on
// type decorated with the ignore attributes
foreach (var prop in ignoredProperties)
model.Properties.Remove(prop.Name);
}
}
SwaggerSpecConfig.Customize(c => c.ModelFilter<OmitIgnoredProperties>());
Run Code Online (Sandbox Code Playgroud)
但我不确定如何配置Swashbuckle在当前版本中使用IModelFilter?我正在使用Swashbuckle 5.5.3.
我正在尝试在单个项目中使用OWIN,SignalR和Autofac.
我正在设置有关signalR的内容如下:
// Create the AutoFac container builder:
var builder = new ContainerBuilder();
// ...[Register various other things in here]...
// register signalR Hubs
builder.RegisterHubs(Assembly.GetExecutingAssembly());
// Build the container:
var container = builder.Build();
// Configure SignalR with the dependency resolver.
app.MapSignalR(new HubConfiguration
{
Resolver = new AutofacDependencyResolver(container)
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我使用Autofac SignalR集成时,我不能再在服务器上获取signalR Hub Context(例如在webapi控制器中),因此无法从服务器端向连接的客户端发送消息.当我不使用Autofac signalR集成时,如下所示:
var hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
hubContext.Clients.All.notification("Test Message");
Run Code Online (Sandbox Code Playgroud)
但是当我将Autofac添加到混合中时,这不起作用 - 我没有得到任何错误消息,我似乎得到了一个hubContext,但是对它的调用实际上似乎并没有到达客户端.
如果我在对MapSignalR的调用中注释掉了对signalR的依赖性解析器的使用,那么对GetHubContext的调用再次起作用并且消息成功地到达signalR客户端,但当然我不能再在我的集线器上使用IoC.例如
// Configure SignalR with the dependency resolver.
app.MapSignalR(new HubConfiguration
{
// Resolver = new AutofacDependencyResolver(container)
});
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么使用AutofacDependencyResolver会阻止GlobalHost.ConnectionManager.GetHubContext正常工作? …
出于语法高亮和着色以及智能感知的目的,可以将其他扩展(tpl,master等)视为HTML吗?
我知道可以在逐个文件的基础上按CTRL + SHIFT + P并选择"更改语言模式"但我希望它能够解决文件扩展问题,而不必每次打开新文件时重做它.
我也知道通过编辑插件目录中的json文件可能有些语言,但似乎没有一个用于HTML.
我非常喜欢新的asp.net(asp.net 5\core 1.0)web应用程序的方法,其中wwwroot文件夹是根目录,只有静态文件才能提供.
可以通过路由或其它配置有一个asp.net 4.5项目的行为一个wwwroot文件夹以类似的方式,这样的静态文件只担任了它,并且它的web应用程序的静态文件的"根"?
(我提出这个问题的部分原因是我在VS2015中的asp.net 5项目中有一个角色应用程序,但是我需要将它转移到asp.net 4.5项目中,但是希望将现有结构保留在磁盘上)
我尝试使用一个空的ASP.NET 4.5 Web应用程序项目和OWIN.所以我的文件夹结构有我的角度应用程序,在项目文件夹的wwwroot文件夹中有一个主index.html文件.项目的根目录中没有HTML文件.
我通过nuget和以下启动文件添加了OWIN:
[assembly: OwinStartup(typeof(MyApp.UI.Startup))]
namespace MyApp.UI
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
string root = AppDomain.CurrentDomain.BaseDirectory;
var physicalFileSystem = new PhysicalFileSystem(Path.Combine(root, "wwwroot"));
var options = new FileServerOptions
{
EnableDefaultFiles = true,
FileSystem = physicalFileSystem
};
options.StaticFileOptions.FileSystem = physicalFileSystem;
options.StaticFileOptions.ServeUnknownFileTypes = false;
options.DefaultFilesOptions.DefaultFileNames = new[] {"index.html"};
app.UseFileServer(options);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这虽然失败 - 我的index.html文件的源确实负载,当我运行它,但所有的CSS,JS等文件,它引用失败有404更糟的是,如果我追加gulpfile.js到根网址是从项目文件夹的根目录加载我的gulp文件.这正是我想要避免的那种事情.
有任何想法吗?
我正在寻找创建一个使用与OpenSSL兼容的.NET库的类.我知道有一个OpenSSL.Net包装器,但我宁愿避免引用第三方\非托管代码.我不是在寻找关于这是否是正确选择的讨论,但有理由.
目前我有以下内容,我认为应该与OpenSSL兼容 - 它有效地做了我认为OpenSSL从OpenSSL文档中所做的事情.但是,即使只使用这个类来进行加密和解密,我也会收到以下错误:
[CryptographicException] Padding is invalid and cannot be removed.
Run Code Online (Sandbox Code Playgroud)
我已经逐步完成了代码并验证了salt\key\iv在加密和解密过程中都是一样的.
请参阅下面的示例类和调用加密解密.欢迎任何想法或指示.
public class Protection
{
public string OpenSSLEncrypt(string plainText, string passphrase)
{
// generate salt
byte[] key, iv;
byte[] salt = new byte[8];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes(salt);
DeriveKeyAndIV(passphrase, salt, out key, out iv);
// encrypt bytes
byte[] encryptedBytes = EncryptStringToBytesAes(plainText, key, iv);
// add salt as first 8 bytes
byte[] encryptedBytesWithSalt = new byte[salt.Length + encryptedBytes.Length];
Buffer.BlockCopy(salt, 0, encryptedBytesWithSalt, 0, salt.Length);
Buffer.BlockCopy(encryptedBytes, 0, encryptedBytesWithSalt, salt.Length, …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用OWIN\Katana进行web api项目.它使用Windows身份验证.这似乎有效,但我的大多数集成测试都已破坏.他们以前刚刚使用内存,HttpServer但我已经改用Microsoft.Owin.Testing.TestServer.我在测试设置中替换了这样的东西:
var config = new HttpConfiguration { IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always };
config.EnableQuerySupport();
Server = new HttpServer(config);
MyConfigClass.Configure(config);
WebApiConfig.Register(config);
Run Code Online (Sandbox Code Playgroud)
更简单:
TestServer = TestServer.Create<Startup>();
Run Code Online (Sandbox Code Playgroud)
但是之前我可以将以下内容用于内存服务器的"伪"身份验证:
Thread.CurrentPrincipal = new ClientRolePrincipal(new HttpListenerBasicIdentity(Username, Password));
Run Code Online (Sandbox Code Playgroud)
现在这不起作用.我得到以下所有请求:
System.Exception : {"Message":"Authorization has been denied for this request."}
Run Code Online (Sandbox Code Playgroud)
如何使用内存中的OWIN测试服务器进行身份验证或至少绕过身份验证?
我目前正在使用jquery-form-observe插件,该插件使用onbeforeunload来提示用户"未保存"的更改.
但我有一个场景,我需要在按钮点击时触发它:按钮点击最终会导致页面更改,但我想在用户启动按钮点击开始的过程之前提示用户...
那么有没有办法通过jQuery或其他方式触发onbeforeunload?
我使用基于以下方法的内容在2个MVC站点(称为SiteA和SiteB)上进行基本单点登录:
http://forums.asp.net/p/1023838/2614630.aspx
它们位于同一域的子域中,并在web.config中共享hash\encryption键等.我已经修改了cookie,因此可以访问同一域中的所有站点.所有这一切似乎都正常.
这些站点位于不同的服务器上,无法访问相同的SQL数据库,因此只有SiteA实际上拥有用户登录详细信息.SiteB有一个成员资格数据库,但用户空白.
这适用于我所需的场景,即:
1)用户登录SiteA
2)应用程序从SiteA(通过AJAX)和SiteB(通过AJAX使用JSONP)加载数据
我在SiteA的AccountController上有以下LogOn Action,这是"魔术"发生的地方:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
//modify the Domain attribute of the cookie to the second level of domain
// Add roles
string[] roles = Roles.GetRolesForUser(model.UserName);
HttpCookie cookie = FormsAuthentication.GetAuthCookie(User.Identity.Name, false);
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
// Store roles inside the Forms cookie.
FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(ticket.Version, model.UserName,
ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, String.Join("|", roles), ticket.CookiePath);
cookie.Value = FormsAuthentication.Encrypt(newticket);
cookie.HttpOnly …Run Code Online (Sandbox Code Playgroud) 我有一个MVC站点,使用[授权]属性进行保护,但在生产网站上有一个问题,即在不同服务器上的一对或多个站点上使用单点登录.我想将身份验证作为原因; 有没有办法暂时关闭通过web.config的身份验证,以便可以访问具有授权属性的所有或一些控制器操作而无需登录?
编辑:
我尝试将以下内容添加到web.config:
<authentication mode="None" />
Run Code Online (Sandbox Code Playgroud)
但这会导致使用"授权属性"修饰的所有操作都呈现空白页面.没有授权的操作会继续有效
c# ×4
asp.net ×3
asp.net-mvc ×3
owin ×3
.net ×1
aes ×1
angular ×1
autofac ×1
dom-events ×1
encryption ×1
javascript ×1
jquery ×1
katana ×1
openssl ×1
signalr ×1
swagger ×1
swashbuckle ×1
web-config ×1