我正在尝试实现具有以下要求的MVC4 Web应用程序:
(a)它仅向经过认证的用户提供服务.至于身份验证,我想使用简单的成员身份,因为它是MVC的最新身份验证技术,使我能够定义自己的数据库表,提供开箱即用的OAuth支持,并且可以轻松地与MVC和的WebAPI.
(b)它通过WebApi为移动/ JS客户端公开一些核心功能,这些功能应通过基本的HTTP身份验证(+ SSL)进行身份验证.通常,我会让JS客户端使用对WebApi控制器的jQuery AJAX调用,使用Authorize属性修饰不同的用户角色.
(c)理想情况下,在混合环境中我想避免双重身份验证:即如果用户已通过浏览器进行身份验证,并且正在访问暗示对WebApi控制器操作进行JS调用的页面,则(a)机制应该是足够.
因此,虽然(a)由默认的MVC模板覆盖,但(b)需要基本的HTTP身份验证而不需要浏览器的中介.为此,我应该创建一个DelegatingHandler,就像我在这篇文章中找到的那样:http://www.piotrwalat.net/basic-http-authentication-in-asp-net-web-api-using-message-handlers.问题是它的实现需要某种方法从收到的用户名和密码中检索IPrincipal,而WebSecurity类没有为此提供任何方法(登录除外,但我会避免仅为了授权而更改记录的用户) ,也因为潜在的"混合"环境,如(c)).因此,似乎我唯一的选择是放弃简单的会员资格.有没有人有更好的建议?以下是引用帖子中的相关(略微修改)代码:
public interface IPrincipalProvider
{
IPrincipal GetPrincipal(string username, string password);
}
public sealed class Credentials
{
public string Username { get; set; }
public string Password { get; set; }
}
public class BasicAuthMessageHandler : DelegatingHandler
{
private const string BasicAuthResponseHeader = "WWW-Authenticate";
private const string BasicAuthResponseHeaderValue = "Basic";
public IPrincipalProvider PrincipalProvider { get; private set; }
public BasicAuthMessageHandler(IPrincipalProvider provider)
{
if (provider == null) throw new …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用凭证流创建一个使用OpenIdDict保护的.NET Core Web API的Angular2 SPA.在为这个问题创建一个repro解决方案时,我还详细介绍了自述文件中的所有步骤,所以希望这篇文章对像我这样的新手有用.请在这些存储库中找到完整的repro解决方案:
服务器端(.NET Core + OpenIdDict),详细说明如何构建自己的:https://github.com/Myrmex/repro-oidang
客户端(Angular2):https://github.com/Myrmex/repro-angoid
至于服务器端,我按照OpenIdDict提供的关于此流程的示例(https://github.com/openiddict/openiddict-samples/blob/master/samples/PasswordFlow).以下是最相关的位Startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddEntityFrameworkSqlServer()
.AddDbContext<CatalogContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Catalog")))
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Catalog")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddOpenIddict<ApplicationDbContext>()
.DisableHttpsRequirement()
.EnableTokenEndpoint("/connect/token")
.EnableLogoutEndpoint("/connect/logout")
.EnableUserinfoEndpoint("/connect/userinfo")
.AllowPasswordFlow()
.AllowRefreshTokenFlow()
.AddEphemeralSigningKey();
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
});
// add my services
// ...
services.AddTransient<IDatabaseInitializer, DatabaseInitializer>();
services.AddSwaggerGen();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory,
IDatabaseInitializer databaseInitializer)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug(); …Run Code Online (Sandbox Code Playgroud) 编辑 我认为强制等待异步调用worker的正确方法是使用Task.Run,如下所示:
await Task.Run(() => builder.Build(dlg.FileName, cts.Token, new Progress(ReportProgress)));
Run Code Online (Sandbox Code Playgroud)
从http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx获得了一些启示.
这应该很容易,但我是async/await的新手,所以请耐心等待.我正在构建一个类库,通过一些长时间运行的操作公开API.在过去,我使用BackgroundWorker来处理进度报告和取消,就像在这个简化的代码片段中一样:
public void DoSomething(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = (BackgroundWorker)sender;
// e.Argument is any object as passed by consumer via RunWorkerAsync...
do
{
// ... do something ...
// abort if requested
if (bw.CancellationPending)
{
e.Cancel = true;
break;
} //eif
// notify progress
bw.ReportProgress(nPercent);
}
}
Run Code Online (Sandbox Code Playgroud)
客户端代码如下:
BackgroundWorker worker = new BackgroundWorker
{ WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
worker.DoWork += new …Run Code Online (Sandbox Code Playgroud) 我是angular-ui的新手并且正在玩它.我正在尝试使用分页指令(http://angular-ui.github.io/bootstrap/#/pagination),但似乎我缺少一些样式,因为我只看到呈现的无序列表,而不是预期的寻呼机UI.我按以下顺序包含以下资源:
1)最新的(RC)bootstrap CSS:http://blog.netdna.com/opensource/bootstrapcdn/bootstrapcdn-now-serving-3-0-0-rc1/
2)angular JS:http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js
3)angular-ui/bootstrap:我从https://github.com/angular-ui/bootstrap/tree/gh-pages包含文件ui-bootstrap-tpls-0.4.0.js
我的HTML示例正文:
<body ng-app="Test">
<div ng-controller="PaginationDemoCtrl" class="well well-small">
<pagination boundary-links="true" num-pages="noOfPages" current-page="currentPage" class="pagination-small" previous-text="'‹'" next-text="'›'" first-text="'«'" last-text="'»'"></pagination>
</div>
<script>
var PaginationDemoCtrl = function ($scope) {
$scope.noOfPages = 7;
$scope.currentPage = 4;
$scope.maxSize = 5;
$scope.setPage = function (pageNo) {
$scope.currentPage = pageNo;
};
};
var app = angular.module("Test", ["ui.bootstrap"]);
</script>
</body>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用简单的模态对话框来编辑Angular应用程序上下文中的列表中的项目.对于模态,我使用UI-Bootstrap(AngularUI),我知道它仍然存在Bootstrap 3的问题,但AFAIK可以用于一些简单的变通方法(除非构建AngularUI的尚未发布的分支).我在这里创建了一个简单的repro Plunker:
http://plnkr.co/edit/MWa3bLMqIkwxmxQ6YDSl
示例代码有几个控制器,一个与open它应该开模态方法,和另一种与save和close方法按钮保存和取消.此外,CSS包含一些解决方案,用于处理Bootstrap 3的AngularUI问题.显示模式对话框,但它不接收从调用控制器传递的参数.要传递此参数(item具有id和名称的虚拟对象),我使用resolve模态打开方法调用中的选项,如:
resolve: {
item: function() {
return angular.copy(item);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,item应在对话框控制器中解析的参数似乎未定义.我在这里缺少什么?谢谢!
编辑:见底部
我是 SignalR 的新手,并尝试使用这个库和 ASP.NET Core Web API 用 Angular7 客户端实现一个简单的场景。我所需要的只是使用 SignalR 来通知客户端 API 控制器方法中一些冗长操作的进度。
经过多次尝试,我达到了显然已建立连接的程度,但是当长任务开始运行并发送消息时,我的客户端似乎没有收到任何内容,网络套接字中也没有出现任何流量(Chrome F12 -网络 - WS)。
我在这里发布了详细信息,这可能对其他新手也有用(完整源代码位于https://1drv.ms/u/s!AsHCfliT740PkZh4cHY3r7I8f-VQiQ)。可能我只是犯了一些明显的错误,但在文档和谷歌搜索中我找不到与我的代码片段完全不同的代码片段。谁能给个提示?
服务器端我的起点是https://msdn.microsoft.com/en-us/magazine/mt846469.aspx,加上文档在https://docs.microsoft.com/en-us/aspnet/core/ signalr/hubs?view=aspnetcore-2.2。我试图用它创建一个虚拟的实验解决方案。
我以食谱形式的代码片段如下。
(A) 服务器端
1.创建一个新的 ASP.NET 核心 Web API 应用程序。没有身份验证或 Docker,只是为了保持最小化。
2.添加NuGet包Microsoft.AspNetCore.SignalR。
3.在Startup.cs,ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
// CORS
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyMethod()
.AllowAnyHeader()
// https://github.com/aspnet/SignalR/issues/2110 for AllowCredentials
.AllowCredentials()
.WithOrigins("http://localhost:4200");
}));
// SignalR
services.AddSignalR();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
Run Code Online (Sandbox Code Playgroud)
以及相应的Configure方法: …
我有一个带有2个模板化列的DataGrid:
第一个包括一个组合框,我让它的大小适合它的内容,因为这最多只有一两个字;
第二个包含一个文本框,文本可能会变得有点长.
所以在这里我设置MaxWidth=somevalue避免它的宽度扩展超出datagrid容器,我为它做同样的事情MaxHeight,并设置要包装的文本.无论如何,我希望文本框的大小调整以填充datagrid容器中的所有剩余空间:如果用户缩小或放大第二列,我希望文本框相应地缩小或放大,以便它们的宽度保持在同步.文本将换行,并根据需要显示滚动条.
网格中的两个控件都绑定到MVVM方案中的数据源.任何人都可以提示让模板文本框宽度与容器列扩展/收缩吗?这是我的示例代码:
<DataGrid ...>
<DataGrid.Columns>
<!-- 1 -->
<DataGridTemplateColumn ...>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox .../>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- 2: THIS TEXTBOX SHOULD EXPAND/CONTRACT WITH ITS CONTAINER COLUMN -->
<DataGridTemplateColumn ...>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox TextWrapping="Wrap"
MinWidth="400" MaxWidth="700"
MaxHeight="400"
ScrollViewer.VerticalScrollBarVisibility="Auto" .../>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud) 更新#1:在我评论的修复之后,现在我的应用程序启动但网格不会呈现,除了它的边界框和过滤器按钮和弹出窗口.然而,我没有从控制台得到任何错误,并且只要我可以使用调试器到达,我可以看到从服务器获得的数据是正常的.如果我使用Batarang,我可以看到与我的模型相对应的范围,正确填充项目.我相应地更新了可下载的repro解决方案.谁能解释为什么ng-grid不在这里更新?
我开始使用ng-grid和TypeScript,一旦我的测试应用程序启动,我就会发现问题.请参阅本文的底部,以获取完整测试解决方案的链接.当然,即使在这几个文件中我也犯了很多错误,但我希望有一些东西可以开始,并逐步学习.
MVC应用程序有两个客户端应用程序:
app.js为默认视图(Home/Index).这里没有打字稿,整个代码在这个单独的文件中是自包含的.该代码源自ng-grid文档中的分页示例,并尽可能保持最简单.
MyApp.js用于另一个视图中的更逼真的样本(Home/Model).此示例使用服务,模型和控制器,其JS代码是从TypeScript编译的.为了简单起见,我只是将这些组件存储在Scripts/App下,存储在控制器,模型和服务的文件夹中,每个文件只包含一个类或接口.生成的JS文件手动包含在视图中.
app.js有效,但它有过滤问题.我在这里发布了这些: 使用ng-grid的服务器端过滤:绑定问题?
MyApp.js有ng-grid的启动问题.应用程序启动后,网格绑定中会抛出一个TypeError:
TypeError: Cannot set property 'gridDim' of undefined
at ngGridDirectives.directive.ngGridDirective.compile.pre (http://localhost:55203/Scripts/ng-grid-2.0.7.js:2708:37)
at nodeLinkFn (http://localhost:55203/Scripts/angular.js:4392:13)
at compositeLinkFn (http://localhost:55203/Scripts/angular.js:4015:15)
at nodeLinkFn (http://localhost:55203/Scripts/angular.js:4400:24)
at compositeLinkFn (http://localhost:55203/Scripts/angular.js:4015:15)
at publicLinkFn (http://localhost:55203/Scripts/angular.js:3920:30)
at resumeBootstrapInternal (http://localhost:55203/Scripts/angular.js:983:27)
at Object.$get.Scope.$eval (http://localhost:55203/Scripts/angular.js:8057:28)
at Object.$get.Scope.$apply (http://localhost:55203/Scripts/angular.js:8137:23)
at resumeBootstrapInternal (http://localhost:55203/Scripts/angular.js:981:15) <div ng-grid="gridOptions" style="height: 400px" class="ng-scope"> angular.js:5754
Run Code Online (Sandbox Code Playgroud)
我通过谷歌搜索找到的唯一类似问题是https://github.com/angular-ui/ng-grid/issues/60,但它似乎与我的情况无关,因为网格选项设置太晚了.
服务器端只有一个API RESTful控制器返回服务器分页,排序和筛选的项目.
你可以在这里找到完整的repro解决方案(只需保存,解压缩和打开;所有依赖项都来自NuGet); 有关更多信息,请参阅readme.txt文件:
只需启动应用程序并单击右上角的MODEL即可运行引发错误的TypeScript应用程序.整个应用程序由1个控制器,1个服务和1个模型组成.
对于像我这样的初学者来说,拥有像这样的简单工作示例会很不错.有人可以帮忙吗?
请在此处找到完整的重现项目:https : //github.com/Myrmex/tinymce-plugin-repro。
找到的解决方案:见底部。
我正在使用 TinyMCE 和我的一个非常简单的自定义插件开发 Angular4 Web 应用程序。这个名为 的插件format-display的目的是显示光标下文本的颜色,映射到一些潜在的含义(例如,红色表示某物,绿色表示其他东西,等等)。该插件仅读取光标下的颜色并显示其映射(如果有)。它在其构建环境中运行良好(在 Typescript 中使用来自https://www.tinymce.com/docs/advanced/yeoman-generator/的 Yeoman 生成器构建):我的问题是它在我的 Angular 应用程序中使用时没有加载。
这是一个自定义插件(未打包在 TinyMCE 包中),我必须使用external_plugins而不是plugins在初始化时将其添加到编辑器中,并将 dist 文件夹 ( dist/format-display)复制到assets/plugins(或其他地方,始终在 下assets)。至于像库中的任何第三方非NPM,我再添加插件的源代码angular-cli.json下scripts。
TinyMCE 插件管理器似乎正确注册了我的外部插件,正如我tinymce.pluginmanager在调试过程中看到的那样,并且调用注册代码没有错误。然而,编辑器似乎没有加载我注册的插件:它的代码没有任何部分被调用。
在调试期间检查我的源代码,我可以找到我的 plugin.js 的两个实例:
我假设第一个来自在angular-cli.jsonunder 中包含插件代码,scripts第二个来自external_plugins编辑器初始化时的属性。然而,如果我从 angular-cli 中删除引用似乎没有任何变化,并且插件仍然没有加载。
另外:我必须补充一点,这种重复似乎没有副作用。我尝试将生成的 plugin.js 代码更改为var plugin_1 = __webpack_require__(0); if (!tinymce.PluginManager.lookup.formatDisplay) { tinymce.PluginManager.add('formatDisplay', plugin_1["default"]); },但问题仍然存在,即使现在我检查时 …
我正在尝试创建一个 docker-compose 脚本来启动具有 PostgreSql 数据库和 ASP.NET Core 6 Web API 的堆栈。为了测试该场景,我使用默认模板创建了一个新的 ASP.NET Core 6 Web API。然后我添加了 NuGet 包Npgsql(6.0.3)和一个仅查询数据库引擎版本的示例控制器,例如:
[ApiController]
[Produces("application/json")]
public class TestController : Controller
{
private readonly IConfiguration _config;
public TestController(IConfiguration config)
{
_config = config;
}
[HttpGet("api/dbversion")]
public IActionResult GetVersion()
{
using NpgsqlConnection conn = new(_config.GetConnectionString("Default"));
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT version();";
return Ok($"Version: {cmd.ExecuteScalar() as string}");
}
}
Run Code Online (Sandbox Code Playgroud)
然后我为此 API 创建了 Docker 映像,并编写了一个 docker-compose 脚本,如下所示:
version: '3.7'
services:
test-db:
image: postgres
# …Run Code Online (Sandbox Code Playgroud) angular ×3
angularjs ×3
c# ×2
.net-6.0 ×1
angular-cli ×1
angular-ui ×1
asp.net ×1
asp.net-core ×1
async-await ×1
datagrid ×1
docker ×1
javascript ×1
ng-grid ×1
openiddict ×1
postgresql ×1
tinymce ×1
typescript ×1
wpf ×1