Tag: asp.net core

ASP.NET Core 2.0禁用自动挑战

将ASP.NET Core项目升级到2.0后,尝试访问受保护的端点不再返回401,而是重定向到(不存在的)端点以尝试让用户进行身份validation。 所需的行为是应用程序只是返回401.以前我会在配置身份validation时设置AutomaticChallenge = false ,但根据这篇文章 ,设置不再相关(实际上它不再存在)。 我的身份validation配置如下: Startup.cs.ConfigureServices(): services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(o => { o.Cookie.Name = options.CookieName; o.Cookie.Domain = options.CookieDomain; o.SlidingExpiration = true; o.ExpireTimeSpan = options.CookieLifetime; o.TicketDataFormat = ticketFormat; o.CookieManager = new CustomChunkingCookieManager(); }); 配置(): app.UseAuthentication(); 如何禁用自动质询,以便在用户未通过身份validation时应用程序返回401?

使用viewmodel中的列表将字典绑定到复选框

你如何正确地绑定一个字典和它的每个键的值复选框? 我可以在HTTPGET中显示它们,但是再次将所选值绑定到HTTPPOST似乎不起作用。 视图模型 public class EditViewModel { public Foo Foo { get; set; } public Dictionary<Bar, List> Matrix { get; set; } } public class BarVersionEditVM { public int ID { get; set; } public string Name { get; set; } public string Version { get; set; } public bool IsSupported { get; set; } […]

从.net核心中的appsettings.json获取价值

不知道我在这里缺少什么,但我无法从我的.net核心应用程序中的appsettings.json获取值。 我的appsettings.json为: { “AppSettings”: { “Version”: “One” } } 启动: public class Startup { private IConfigurationRoot _configuration; public Startup(IHostingEnvironment env) { _configuration = new ConfigurationBuilder() } public void ConfigureServices(IServiceCollection services) { //Here I setup to read appsettings services.Configure(_configuration.GetSection(“AppSettings”)); } } 模型: public class AppSettings { public string Version{ get; set; } } 控制器: public class […]

如何使用Asp.Net Core实现基于权限的访问控制

我正在尝试使用aspnet核心实现基于权限的访问控制。 为了动态管理用户角色和权限(create_product,delete_product等),它们存储在数据库中。 数据模型类似于http://sofzh.miximages.com/c%23/CHMPE.png 在aspnet核心之前(在MVC 5中)我使用如下的自定义AuthorizeAttribute来处理问题: public class CustomAuthorizeAttribute : AuthorizeAttribute { private readonly string _permissionName { get; set; } [Inject] public IAccessControlService _accessControlService { get; set; } public CustomAuthorizeAttribute(string permissionName = “”) { _permissionName = permissionName; } public override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext); var user = _accessControlService.GetUser(); if (PermissionName != “” && !user.HasPermission(_permissionName)) { […]

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。

ASP.NET 5添加WCF服务引用

在Visual Studio 2015预览版(预发行版)中,如何为WCF服务添加服务引用?

MVC 6 Controller中的ControllerContext和ViewEngines属性在哪里?

我创建了一个新的MVC6项目并构建了一个新站点。 目标是获取视图的渲染结果。 我找到了以下代码,但我无法让它工作,因为我找不到ControllerContext和ViewEngines 。 这是我想要重写的代码: protected string RenderPartialViewToString(string viewName, object model) { if (string.IsNullOrEmpty(viewName)) viewName = ControllerContext.RouteData.GetRequiredString(“action”); ViewData.Model = model; using (StringWriter sw = new StringWriter()) { ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); viewResult.View.Render(viewContext, sw); return sw.GetStringBuilder().ToString(); } }

如何将ASP.NET 5(vNext)用户机密注入我自己的实用程序类?

我已经用过了: user-secret set “AWSAccessKey” “my access key”和 user-secret set “”AWSSecretKey” “my secret key” 让我的钥匙进入秘密商店。 我有这个课程,我将用它来通过Amazon SES发送电子邮件,但我不知道如何将我的密钥插入其中。 public class MailHelper { public void SendEmailSes(string senderAddress, string receiverAddress, string subject, string body) { using (var client = new AmazonSimpleEmailServiceClient( new BasicAWSCredentials(“[AWSAccessKey]”, “[AWSSecretKey]”), RegionEndpoint.USEast1)) { var sendRequest = new SendEmailRequest { Source = senderAddress, Destination = new Destination […]

我应该如何将一个DbContext实例注入IHostedService?

题 我应该如何(使用标准dependency injection)将DbContext实例注入IHostedService ? 我试过了什么 我目前让我的IHostedService类在构造函数中获取MainContext (从DbContext )实例。 当我运行应用程序时,我得到: 无法从singleton’Microsoft.Extensions.Hosting.IHostedService’中使用作用域服务“Microsoft.EntityFrameworkCore.DbContextOptions”。 所以我尝试通过指定使DbContextOptions瞬态: services.AddDbContext(options => options.UseSqlite(“Data Source=development.db”), ServiceLifetime.Transient); 在我的Startup课程中。 但是错误仍然是相同的,即使根据解决的Github问题 ,传递的DbContextOptions应该具有在AddDbContext调用中指定的相同生命周期。 我无法使数据库上下文成为单例,否则对它的并发调用将产生并发exception(由于数据库上下文不保证是线程安全的事实)。

error handling(将ex.Message发送到客户端)

我有一个ASP.NET Core 1.0 Web API应用程序,并尝试弄清楚如果我的控制器调用的函数错误输出exception消息到客户端。 我尝试了很多东西,但没有实现IActionResult 。 我不明白为什么这不是人们需要的常见事情。 如果真的没有解决方案可以有人告诉我为什么? 我确实使用HttpResponseException(HttpResponseMessage)看到了一些文档,但为了使用它,我必须安装compat shim。 在Core 1.0中有没有新的方法来做这些事情? 这是我一直在尝试垫片,但它不起作用: // GET: api/customers/{id} [HttpGet(“{id}”, Name = “GetCustomer”)] public IActionResult GetById(int id) { Customer c = _customersService.GetCustomerById(id); if (c == null) { var response = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(“Customer doesn’t exist”, System.Text.Encoding.UTF8, “text/plain”), StatusCode = HttpStatusCode.NotFound }; throw new […]