我已经搜遍了如何UserService在asp.net核心中注册一个IdentityServer4,但我似乎无法找到正确的方法来做到这一点.
这是注册发现InMemoryUsers代码在这里,但是我想从样品中定义我的MSSQL数据库不是静态的用户访问的用户.
var builder = services.AddIdentityServer(options =>
{
options.SigningCertificate = cert;
});
builder.AddInMemoryClients(Clients.Get());
builder.AddInMemoryScopes(Scopes.Get());
builder.AddInMemoryUsers(Users.Get());
Run Code Online (Sandbox Code Playgroud)
那么我看了一下这是针对IdentityServer3的.
var factory = new IdentityServerServiceFactory()
.UseInMemoryClients(Clients.Get())
.UseInMemoryScopes(Scopes.Get());
var userService = new UserService();
factory.UserService = new Registration<IUserService>(resolver => userService);
Run Code Online (Sandbox Code Playgroud)
从在线阅读看起来我需要使用DI系统注册UserService,但我不确定它如何绑定到IdentityServer,例如.
services.AddScoped<IUserService, UserService>();
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
如何将我绑定UserService到构建器(IdentityServer4用户)?我将如何调用我的数据库来访问和验证我现有的数据库用户UserService(我使用存储库连接到数据库)?
考虑到这一点必须与asp.net核心一起使用.
谢谢!
我正在使用EF7进行标准CRUD操作,使用Dapper进行需要提高速度的更复杂查询.从startup.cs我将我的注入DbContext到我的DAL中,这显然是数据库查询.Dapper需要连接字符串.我想将我的EF7 DbContext连接字符串注入Dapper查询.
如何像以前一样从DbContext获取连接字符串:DbContext.Database.Connection?
它从EF7 变为Database了DatabaseFacade类型,并且DbConnection Connection也被删除了.
当然应该有一些DbContext我可以查询的持久连接字符串?
我现在使用的方法是,它有效:
public partial class CustomContext : DbContext
{
public readonly string _connectionString;
public CustomContext (DbContextOptions options)
: base(options)
{
_connectionString = ((SqlServerOptionsExtension)options.Extensions.First()).ConnectionString;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道它还处于测试阶段,但我错过了一些东西吗?
谢谢你的时间.
使用Entity Framework 7和MVC6时,我似乎收到此错误消息
System.InvalidOperationException未配置任何数据库提供程序.在设置服务时,通过在DbContext类或AddDbContext方法中覆盖OnConfiguring来配置数据库提供程序.
我相信我已经做了我应该做的一切,所以也许它是一个错误.我使用的是Entity Framework的7.0.0-beta7版本.
我已经设置了我的DbContext,一个接口,所以我可以模拟DbContext(在EntityFramework 6中需要进行单元测试).我的服务将接口作为构造函数,我在MVC 6中设置了DI.
在我的Startup.cs文件中,我有以下内容
public void ConfigureServices(IServiceCollection services)
{
// entity framework
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration["Data:MyConnection:ConnectionString"])
);
// Add MVC services to the services container.
services.AddMvc();
// new context on each request
services.AddScoped<IMyDbContext, MyDbContext>();
}
Run Code Online (Sandbox Code Playgroud)
我已经检查了我的connectionString,并且返回了一个有效的连接.我还检查了我的服务,正在注入该对象,并且它不是null,因此应该全部工作.
我的config.json文件看起来像这样
{
"Data": {
"MyConnection": {
"ConnectionString": "Server=(local);Database=XXXX;Trusted_Connection=True;"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的DbContext不会覆盖OnConfiguring方法,因为我认为不需要它,因为我正在完成上述所有操作?我对吗?我错过了什么?看了很多不同的网站,我猜有些人正在使用旧代码,因为有些方法不存在,而其他网站与我拥有的相同.
c# entity-framework entity-framework-core asp.net-core-mvc asp.net-core
我想有一个错误页面,根据提供的查询字符串显示一个稍微不同的错误消息给用户.
在创建新的asp.net 5项目时,我注意到Startup.cs文件中的以下代码.
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
Run Code Online (Sandbox Code Playgroud)
发生异常时,我能够显示正确的错误页面.我的问题是,它似乎只能捕获我的应用程序中尚未处理的错误,即状态代码始终为500.它是否正确?
要处理404错误,我使用以下代码:
app.UseStatusCodePagesWithReExecute("/Error/{0}");
Run Code Online (Sandbox Code Playgroud)
我的控制器实现为:
[HttpGet("{statusCode}")]
public IActionResult Error(int statusCode)
{
return View(statusCode);
}
Run Code Online (Sandbox Code Playgroud)
这似乎捕获404错误并显示正确的状态代码.
如果我在上面的if语句中更新我的代码以使用相同的操作,例如:
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error/{0}");
}
Run Code Online (Sandbox Code Playgroud)
返回的状态代码始终为0.
此外,当会发生什么400,403或其他任何发生?他们会被抓住吗?如果是这样,他们会在什么时候被抓住?
你可以告诉我,我很困惑,并希望有人为我提供一个处理所有不同状态代码的例子.
c# exception http-status-codes asp.net-core-mvc asp.net-core
如何获取上传图像的文件流(IFormFile)并调整其大小?
public ActionResult Upload(IFormFile file)
{
using (var reader = new StreamReader(file.OpenReadStream()))
{
var fileContent = reader.ReadToEnd();
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
//scale image here?
}
}
Run Code Online (Sandbox Code Playgroud) ResponseCache有点替代OutputCache; 但是,我想做服务器端缓存以及每个参数输入.
根据这里和这里的一些答案,我应该使用IMemoryCache或者IDistributedCache这样做.我特别感兴趣的是在参数不同的控制器上进行缓存,之前在asp.net 4中使用过OutputCache,VaryByParam如下所示:
[OutputCache(CacheProfile = "Medium", VaryByParam = "id", Location = OutputCacheLocation.Server)]
public ActionResult Index(long id)
{
///...
}
Run Code Online (Sandbox Code Playgroud)
我将如何在asp.net核心中复制这个?
当我在同一服务上同时启用 JWT 承载身份验证和 CORS 时,我在从服务器获取适当的 Access-Control-Allow-Origin 标头时遇到问题。当我从配置中删除 UseJwtBearerAuthentication 时,一切正常。
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins", builder =>
{
builder.AllowAnyOrigin();
builder.AllowAnyHeader();
builder.AllowAnyMethod();
builder.AllowCredentials();
});
});
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseJwtBearerAuthentication(options =>
{
options.AutomaticAuthenticate = true;
options.RequireHttpsMetadata = false;
options.Audience = "c2cf422a-a432-2038-b183-cda64e16239e";
options.Authority = "domain.com";
});
app.UseCors("AllowAllOrigins");
app.UseIISPlatformHandler();
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
我试图更改配置的顺序,但似乎没有任何效果。我还尝试将 [EnableCors("AllowAllOrigins")] 添加到我正在调用的控制器中。 …
在我的数据表中,我有两行多列。
其中一列是ItemID,它们的值为2215,2216。
当我使用以下语句时:
DataRow[] dr = dt.Select("ItemID='2215'");
Run Code Online (Sandbox Code Playgroud)
它返回两行。这不是过滤。
我缺少什么?
asp.net-core ×6
c# ×6
asp.net-4.6 ×1
cors ×1
datatable ×1
exception ×1
oauth-2.0 ×1
sql-server ×1