我创建了一个.NET Core MVC应用程序,并使用依赖注入和存储库模式将一个存储库注入我的控制器.但是,我收到一个错误:
InvalidOperationException:尝试激活"WebApplication1.Controllers.BlogController"时,无法解析类型"WebApplication1.Data.BloggerRepository"的服务.
型号(Blog.cs)
namespace WebApplication1.Models
{
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
DbContext(BloggingContext.cs)
using Microsoft.EntityFrameworkCore;
using WebApplication1.Models;
namespace WebApplication1.Data
{
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
存储库(IBloggerRepository.cs和BloggerRepository.cs)
using System;
using System.Collections.Generic;
using WebApplication1.Models;
namespace WebApplication1.Data
{
internal interface IBloggerRepository : IDisposable
{
IEnumerable<Blog> GetBlogs();
void InsertBlog(Blog blog);
void …Run Code Online (Sandbox Code Playgroud) 我一直在尝试将 Identity Server 4 合并到我的 ASP.NET Core 3 应用程序中,但不断收到以下错误:
没有为方案“Identity.Application”注册登录身份验证处理程序。
注册的登录方案有: Cookie。你忘记打电话了吗AddAuthentication().AddCookies("Identity.Application",...)?我不确定这个错误是什么意思。
我查看了这个SO问题,这篇(在ASP.NET Core中使用特定方案授权) MS .NET文章以及其他几篇文章,但没有一个有帮助。
我的Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services
.AddAuthentication(x =>
{
x.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme) // , opt => opt.LoginPath = "/Identity"
.AddJwtBearer(opt =>
{
opt.RequireHttpsMetadata = false;
opt.SaveToken = true;
opt.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = …Run Code Online (Sandbox Code Playgroud) 将我的网站从 .NET Core 2.2 迁移到 3.1.1 后,我的 api 端点突然开始尝试将我的 api 请求重定向到默认登录页面(/Account/Login?ReturnUrl=,我的任何路由中都没有该页面)。
我的 api 使用 JWT 承载身份验证方案和 JWT 挑战方案,但仍然发生了重定向。
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
Run Code Online (Sandbox Code Playgroud)
我终于找到了问题的解决方案,但我不知道为什么它实际上有帮助。
最初我的服务设置为:
services
.AddIdentity<IdentityUser, IdentityRole>()
.AddSignInManager()
.AddEntityFrameworkStores<CleWebToolsIdentityDbContext>();
Run Code Online (Sandbox Code Playgroud)
但这确实实现了重定向。
最终解决我的问题的是将它们设置为:
services
.AddIdentityCore<IdentityUser>()
.AddRoles<IdentityRole>()
.AddSignInManager()
.AddEntityFrameworkStores<CleWebToolsIdentityDbContext>();
Run Code Online (Sandbox Code Playgroud)
有人能告诉我这是怎么回事吗?
即使质询方案应该是 JWT,AddIdentity 方法如何导致重定向?
我是 ASP.NET Core 的新手。我尝试使用不和谐作为第 3 方登录服务登录(例如使用 facebook、google 登录)。
我可以成功登录并拥有我的用户对象、声明,并且我可以输入具有授权属性的类。下面你可以看到 UserIdentity 没问题。
但假设用户想要返回登录页面。在这种情况下,我必须将他重定向到索引,但我想通过使用 Identity 检查用户是否经过身份验证,不幸的是,它是错误的并且没有声明等。据我了解,它可能与 cookie 或其他东西有关相似的。我还对类使用不同的属性(不是授权而是允许匿名)你可以在我的 Identity 对象下面看到
我正在分享我的验证码
services.AddAuthentication(options =>
{
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.Cookie.MaxAge = options.ExpireTimeSpan;
options.SlidingExpiration = true;
options.EventsType = typeof(CustomCookieAuthenticationEvents);
options.AccessDeniedPath = "/auth/DiscordAuthFailed";
})
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration.GetValue<string>("Jwt:Issuer"),
ValidAudience = Configuration.GetValue<string>("Jwt:Audience"),
IssuerSigningKey = new …Run Code Online (Sandbox Code Playgroud)