我试图将ASP .NET Identity Core与身份服务器4一起使用。
信息:xena.IdentityServer.controllers.accountcontroller [0] 用户登录。
我的登录控制器然后将用户发送到我的管理控制器。
[Route("[controller]/[action]")]
[Authorize]
//[Authorize(AuthenticationSchemes = "Identity.Application")]
public class ManageController : Controller
{
[HttpGet]
public async Task<IActionResult> Index(ManageMessageId? message = null)
{
.....
}
}
用户永远不会到达,因为登录因某种原因而丢失。
info: Microsoft.AspNetCore.Mvc.RedirectToActionResult[2]
Executing RedirectResult, redirecting to /Manage/Index.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action Xena.IdentityServer.Controllers.AccountController.Login (Xena.IdentityServer) in 3493.6102ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 3515.9158ms 302
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request starting HTTP/1.1 GET http://localhost:5000/Manage/Index
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
Authorization failed for user: (null).
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
info: Microsoft.AspNetCore.Mvc.ChallengeResult[2]
Executing ChallengeResult with authentication schemes ().
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[12]
AuthenticationScheme: Bearer was challenged.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action Xena.IdentityServer.Controllers.ManageController.Index (Xena.IdentityServer) in 46.2285ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 58.6793ms 401
我认为线索的一部分是这条线Authorization failed for user: (null).
,我可以看到cookie在浏览器中。它只是没有被阅读。
据我了解,身份服务器4具有其自己的cookie,而ASP .NET Core Identity具有它,他们需要阅读相同的cookie。我尝试关注使用ASP.NET核心身份,但没有帮助。
Identity Server项目中的启动
//Adds Asp.net identity
services.AddDbContext<ApplicationDbContext>(builder =>
builder.UseSqlServer(Configuration.GetConnectionString("XenaIdentityConnection")));
// Configuer Asp.net Identity
services.AddIdentity<ApplicationUser, IdentityRole<long>>(config =>
{
config.Password.RequireDigit = true;
config.Password.RequireLowercase = true;
config.Password.RequireNonAlphanumeric = false;
config.Password.RequireUppercase = true;
config.Password.RequiredLength = 8;
config.User.RequireUniqueEmail = true;
config.SignIn.RequireConfirmedEmail = true;
config.SignIn.RequireConfirmedPhoneNumber = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager<ApplicationSignInManager>() // Adds custom SignIn manager.
.AddDefaultTokenProviders();
//https://identityserver4.readthedocs.io/en/release/topics/signin.html
services.AddAuthentication(options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
})
.AddGoogle("Google", options =>
{
options.AccessType = "offline";
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.ClientId = "xxxxx-jvu30c2n19thoqimd97b4jk1r2poh17p.apps.googleusercontent.com";
options.ClientSecret = "g29nXgVoFZBIBNS-hJJxPWXW";
}).AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, "OpenID Connect", options =>
{
options.SaveTokens = true;
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.SignOutScheme = IdentityServerConstants.SignoutScheme;
options.RequireHttpsMetadata = settingsSetup.RequireHttpsMetadata;
options.Authority = settingsSetup.Authority;
options.ClientId = "testclient";
options.Scope.Add("testapi");
options.ResponseType = OpenIdConnectResponseType.IdTokenToken;
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
});
services.AddIdentityServer()
.AddSigningCredential(LoadCertificate())
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(Configuration.GetConnectionString("XenaIdentityConnection"),
sql => sql.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name));
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(Configuration.GetConnectionString("XenaIdentityConnection"),
sql => sql.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name));
})
.AddAspNetIdentity<ApplicationUser>()
.AddProfileService<ProfileService>();
添加以下内容确实可以解决我的管理/索引问题。但是它不起作用,因为这样的打开ID连接登录将无法使用,因为它使用Identity Server中的内部控制器进行身份验证,而我不能/不想超载。我需要弄清楚如何获得Identity Server 4来使用Aspnet Identity设置的cookie,反之亦然。
//[授权(AuthenticationsChemes =" Indentity.Application")]
这个解决方案来自我在堆栈上提出的一个问题,因为我倾向于这是身份服务器的设置问题,而不是Identity Cookies
我终于弄清楚了今天早上的问题。问题的一部分是由于我有一个使用IdentityCocie的自定义标志管理器。
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
})
需要设置DefaultAuthenticateScheme和DefaultChallenGescheme。然后一切都可以正常工作。