在ASP.NET核心标识中添加声明很简单,但我无法找到如何添加基于临时会话的声明.
我的用例是,具有自己的自定义声明的登录用户需要定期添加新声明,但仅限于当前会话,而不是持久保存到数据库.关闭浏览器或退出后,临时声明应该消失.
这是为了满足一种机制,该机制临时更改用户所属的组织,以便在我正在迁移到asp.net Core的应用程序中进行模拟.
我在这里找到了很多关于GenerateUserIdentityAsync在以前版本的Identity中实现这一点的建议,但在新版本中似乎没有.
c# asp.net asp.net-mvc asp.net-identity asp.net-core-identity
ASP.NET Core文章标题为“ 基于自定义策略的授权 ”的示例代码将AuthorizationContext参数传递到HandleRequirementAsync()方法中。
有人可以说明AuthorizationContext与AuthorizationHandlerContext以及如何实现吗?
我有两个使用相同的ASP.NET核心身份后端的服务器.我使用以下代码生成密码重置令牌:
var token = await _userManager.GeneratePasswordResetTokenAsync(applicationUser);
Run Code Online (Sandbox Code Playgroud)
我通过电子邮件链接发送此令牌.当用户单击该链接时,它们将被带到一个单独的站点,该站点应提供用于更改密码的UI.以下代码处理用户提交令牌及其新密码的密码:
var identityResult = await _userManager.ResetPasswordAsync(applicationUser, code, password);
Run Code Online (Sandbox Code Playgroud)
在第二台服务器上,标识结果始终返回false,因为"invalid token".
通过源查看,我看到令牌是使用IP地址生成的(所以我理解为什么令牌验证失败).
我的问题是如何在不同的机器上启用成功的令牌创建/验证?在以前的ASP.NET形式中,我可能会使用共享机器密钥来阻止这些情况.ASP.NET Core似乎没有类似的概念.根据我的阅读,似乎这可能是使用DataProtection API的场景.不幸的是,我没有看到任何关于如何应用它来生成重置令牌的示例.
我有一个运行 .net core 2.x 的 Web 应用程序(最终刚刚从 .net core 1.x 升级),并且我已经将大部分内容从 .net core 1.x 移植到 2.x。然而,我的身份实现遇到了砖墙。它在 .net core 1.x 上运行良好,但现在它拒绝了。
该实现在 Entity Framework Core 上运行,并首先构建数据库(在预先存在的数据库中实现)。
我的问题是,当我现在尝试使用 .net core 2.x 登录时,我收到一条错误消息,指出:
InvalidOperationException:从 'AspNetUserRole.AspNetRole' 到具有外键属性 {'RoleId' : int} 的 'AspNetUserRole.AspNetRole' 的关系无法定位主键 {'Id' : int},因为它不兼容。为此关系配置一个主键或一组兼容的外键属性。
对我来说,这完全没有意义。int外键如何与int主键不兼容?
上下文和类的实际实现如下(这是一个非常简单的实现):
public partial class AspNetUser : IdentityUser<int>
{ }
public partial class AspNetRole : IdentityRole<int>
{ }
public partial class AspNetRoleClaim : IdentityRoleClaim<int>
{ }
public partial class AspNetUserClaim : IdentityUserClaim<int>
{ } …Run Code Online (Sandbox Code Playgroud) c# invalidoperationexception entity-framework-core asp.net-core asp.net-core-identity
在ASP.NET Core 2(带有或不带有Identity)中使用Cookie身份验证时,可能会发生以下情况:更改用户的电子邮件或名称,甚至在cookie的生存期内删除帐户。这就是为什么文档指出cookie应该被验证的原因。文档中的示例带有注释
此处描述的方法是在每个请求上触发的。这可能会导致应用程序性能下降。
所以我想知道验证cookie主体的最佳模式是什么。我所做的Startup.cs是订阅OnValidatePrincipal事件并通过例如LastValidatedOn对cookie 附加声明来每隔5分钟检查一次主记录的有效性:
services.ConfigureApplicationCookie(options =>
{
// other cookie options go here
options.Events.OnValidatePrincipal = async context =>
{
const string claimType = "LastValidatedOn";
const int reValidateAfterMinutes = 5;
if (!(context.Principal?.Identity is ClaimsIdentity claimIdentity)) return;
if (!context.Principal.HasClaim(c => c.Type == claimType) ||
DateTimeOffset.Now.UtcDateTime.Subtract(new DateTime(long.Parse(context.Principal.Claims.First(c => c.Type == claimType).Value))) > TimeSpan.FromMinutes(reValidateAfterMinutes))
{
var mgr = context.HttpContext.RequestServices.GetRequiredService<SignInManager<ApplicationUser>>();
var user = await mgr.UserManager.FindByNameAsync(claimIdentity.Name);
if (user != null …Run Code Online (Sandbox Code Playgroud) 我试图让IdentityServer4在新的.NET Core 2.1应用程序中运行(它在.NET Core 2.0应用程序中完美运行).我尝试过以下方法:
1)下载此项目,即IdentityServer4应用程序:https://github.com/ghstahl/IdentityServer4-Asp.Net-2.1-Identity-Examples/tree/e0aeeff7e078aa082c8e16029dd2c220acc77d7b
2)使用Identity Server4应用程序下载此项目,即MVC应用程序:https://github.com/IdentityServer/IdentityServer4.Samples/tree/dev/Quickstarts/6_AspNetIdentity/src/MvcClient.
3)将两个项目添加到同一解决方案中.MVC项目使用IdentityServer项目进行身份验证; 授权等
我不得不做出以下改变:
1)更改为IdentityServer应用程序中包含的Startup(AddIdentityServer现在接受参数):
services.AddIdentityServer(options =>
{
options.UserInteraction.LoginUrl = "/Identity/Account/Login";
options.UserInteraction.LogoutUrl = "/Identity/Account/Logout";
})
Run Code Online (Sandbox Code Playgroud)
2)配置IdentityServer应用程序以侦听端口5000并在身份服务器上禁用SSL.
除了注销工具外,一切都按预期开箱即用.当我在MVC应用程序中单击注销时; 在MVC应用程序中调用以下代码:
public async Task Logout()
{
await HttpContext.SignOutAsync("Cookies");
await HttpContext.SignOutAsync("oidc");
}
Run Code Online (Sandbox Code Playgroud)
然后,用户将重定向到IdentityServer应用程序中的Logout.cshtml.但是,他们必须再次单击注销(在IdentityServer应用程序上)才能实际注销,即他们在MVC应用程序中单击注销(第二点),然后在IdentityServer中注销(第一点).
为什么最终用户必须注销两次?
如何仅使用默认本地登录而不使用外部登录提供程序来配置 asp.net core mvc 2.2?
我似乎找不到删除它的方法:
Use another service to log in.
There are no external authentication services configured. See this article for details on setting up this ASP.NET application to support logging in via external services.
Run Code Online (Sandbox Code Playgroud)
我现在只想使用本地帐户(也许稍后会添加外部提供商)。
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddDefaultUI(UIFramework.Bootstrap4);
Run Code Online (Sandbox Code Playgroud) 我正在使用.NET Core 3.0.100; Microsoft Visual Studio Community 2019预览版本16.4.0预览版1.0; Blazor服务器(官方发行版)。
我正在尝试将身份验证和授权添加到我的Blazor服务器Web应用程序。我在这里阅读指南https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-3.0&tabs=visual-studio#scaffold-identity-into-an -空项目
(我也读过这个https://github.com/aspnet/Identity/issues/1825)
然后,我右键单击Project,选择Add\New Scaffolded Item...
我读了文件ScaffoldingReadme.txt,然后按照指南进行操作。
我按F5进行调试,发现错误
Severity: Error
Error Code: CS0246
Description: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?)
Project: foo
File: C:\Users\donhuvy\Desktop\foo\bar\obj\Debug\netcoreapp3.0\Razor\Pages\Shared\_Layout.cshtml.g.cs
Line: 455
Suppression State: Active
Run Code Online (Sandbox Code Playgroud)
屏幕截图[![在此处输入图片描述] [4]] [4]
由于文件\obj\Debug\netcoreapp3.0\Razor\Pages\Shared\_Layout.cshtml.g.cs位于Razor类库中,Microsoft.AspNetCore.Identity.UI (3.0.0)因此我无法对其进行编辑。
如何解决?
asp.net-core asp.net-core-identity blazor asp.net-core-3.0 .net-core-3.0
我有一个 ASP.NET Core 3.1 项目,我想在我的应用程序中实现 Identity。我发现了很多 EF Core 的示例,但我使用的是 LinqConnect (Devart),而不是 EF Core 和数据库 - SQL Server。我想知道如何以简单的方式在我的项目中实现身份。
c# asp.net-identity asp.net-core asp.net-core-identity asp.net-core-3.1
我将声明添加到声明主体身份并让用户登录。在后续请求中,添加的声明可在应用程序中任何位置的声明主体中使用,但只能持续 25 分钟。我没有测试过25到30分钟。30 分钟后,声明主体身份仍经过身份验证,但仅包含来自身份数据库的声明。登录时添加的“CookieClaim”丢失。声明主体身份“IsAuthenticated”仍然是 true,并且导航菜单等中的问候语仍然是“Hi emailaddress”。如果我们在20分钟内提出请求也没关系。
我希望只要用户登录,这些声明就可用。
该应用程序使用 OAuth 从多个外部提供商获取用户的访问令牌和其他信息,该信息用于整个应用程序的授权。我选择将声明中的信息放入cookie中,因为它可能会定期更改,并且不适合存储在Identity数据库中。
以下代码来自我用来演示的 asp.net core 2.2 应用程序的支架 LoginModel。它与我的主应用程序不完全相同,但添加了声明,输入“CookieClaim”并使用相同的方法登录。从“IdentityUser user...”开始的四行实际上是我对模板 asp.net core Web 应用程序所做的唯一更改(使用本地用户帐户,在搭建“登录”页面之后)
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
IdentityUser user = await _signInManager.UserManager.FindByEmailAsync(Input.Email);
ClaimsPrincipal currentUser = …Run Code Online (Sandbox Code Playgroud)