我试图在控制器的构造函数中设置一个属性,如下所示:
public ApplicationUserManager UserManager { get; private set; }
public AccountController()
{
UserManager = HttpContext.GetOwinContext().Get<ApplicationUserManager>("");
}
Run Code Online (Sandbox Code Playgroud)
但正如这里所解释的:
HttpContext 在构造函数中不可用。
那么如何设置该属性以便我可以在控制器的每个操作中访问它呢?
使用 ASP.NET MVC5、EF6 和 Ninject 作为后端,AngularJS 作为前端,使用基于令牌的身份验证 (JWT)。
我们最近不得不在用户名中启用@chars。基于Startup.cs 中的这个答案(由 Ninject 注册代码调用,见下文),我们替换了
UserManagerFactory = () => new ApplicationUserManager(new UserStore<IdentityUser>(new SecurityDbContext()));
Run Code Online (Sandbox Code Playgroud)
和
var userManager = new ApplicationUserManager(new UserStore<IdentityUser>(new SecurityDbContext()));
var validator = new UserValidator<IdentityUser>(userManager)
{
AllowOnlyAlphanumericUserNames = false
};
userManager.UserValidator = validator;
UserManagerFactory = () => userManager;
Run Code Online (Sandbox Code Playgroud)
这允许根据需要使用@ 符号注册用户名。但是,登录应用程序(即使使用“普通”用户名)变得有问题:虽然服务器启动后的第一次登录照常工作,但任何后续登录都会产生以下异常:
Cannot access a disposed object.
Object name: 'ApplicationUserManager'.
Run Code Online (Sandbox Code Playgroud)
详细的错误信息:
源错误:
第 18 行:public override async Task FindAsync(string userName, string password)
第 19 行:{
第 20 行:var result = await base.FindAsync(userName, … 我正在使用Identity 2.1来处理我的asp.net应用程序中的用户角色.到目前为止,我创建了从IdentityDBContext扩展的新上下文,扩展了IdentityUser和IdentityRole以添加几个新字段.但是,每当我尝试使用UserManager即可将用户添加到特定角色时The entity type IdentityRole is not part of the model for the current context..所以用户 - 角色关系似乎有问题,到目前为止这是我的代码供参考:
用户
public class User : IdentityUser{
public string Name { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager, string authenticationType) {
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
}
Run Code Online (Sandbox Code Playgroud)
角色
public partial class Role : IdentityRole
{
public string Description { get; …Run Code Online (Sandbox Code Playgroud) 在Windows Identity Framework(WIF)中,您可以实施ClaimsAuthenticationManager以修改主体上的声明或向其添加新声明。
声明身份验证管理器在应用程序的声明处理管道中提供了一个可扩展性点,您可以在执行RP应用程序代码之前,使用该点来验证,过滤,修改传入的声明或将新声明插入到ClaimsPrincipal提出的声明集中。
ASP.net Identity 2是否具有这样的管道挂钩?如果我想添加一些声明而不将其保留在AspNetUserClaims表中,该怎么办?
默认情况下,ASP.NET Identity将UserName和Email字段作为用户输入的电子邮件.我已经更改了它,以便我的系统为用户提供单独的用户名和电子邮件.
我的问题是现在我无法登录系统,因为Identity对UserName和Email都进行了检查.
我想知道是否有办法让这两个字段保持独立和独特,同时能够检查电子邮件字段以便登录系统?
我注意到public virtual Task<SignInStatus> PasswordSignInAsync(string userName, string password, bool isPersistent, bool shouldLockout);检查了userName,但是将其更改为电子邮件什么也没做,我仍然无法登录.
我还看了其他这些问题:
对于可能的解决方案,发现没有什么能让我做我想做的事.
还有什么我可以尝试以便拥有单独的用户名和电子邮件,同时仍然能够使用电子邮件字段登录吗?
我是Asp.net开发人员,但对Asp.net Identity框架非常陌生.我一直在研究示例应用程序,并且在Identity上也遵循了一些教程,但我仍然无法完全掌握这个概念.我非常坚定地掌握Asp.net会员资格,但身份似乎与会员资格无关.我会解释到目前为止我做了什么.
我正在创建一个简单的应用程序,其中我遵循代码第一种方法.我为User创建了实体模型,它继承自IdentityUser并且有一些额外的字段.以下是User的实体模型.
public class User : IdentityUser
{
public int? CompanyID { get; set; }
public bool? CanWork { get; set; }
public bool? CanSearch { get; set; }
public Company Company { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在在示例中,人们使用名称ApplicationUser,但出于我自己的目的,我使用了名称User.User或ApplicationUser模型中还有一个方法,
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
{
CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
Run Code Online (Sandbox Code Playgroud)
我无法理解这种方法的目的.同样从一个例子我使用以下模型的角色,
public class Role : IdentityRole
{
public Role()
{
}
public Role(string roleName, string description)
: base(roleName)
{ …Run Code Online (Sandbox Code Playgroud) 我扩展了IdentityUserRole,如下所示
public class ApplicationUserRoles : IdentityUserRole<string>
{
[Key]
public string ApplicationId { get; set; }
public virtual AspNetApplications AspNetApplications { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的AspNetApplications类如下
public class AspNetApplications
{
[Key]
public string ApplicationId { get; set; }
public string ApplicationName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
迁移已在DB中创建了AspNetApplications和ApplicationUserRoles表.屏幕截图如下.
以下是我的身份模型
public class ApplicationUser : IdentityUser
{
public virtual AspNetApplications AspNetApplication { get; set; }
public virtual ApplicationUserRoles AspNetUserRoles { get; set; }
//public virtual ICollection<AspNetApplicationUsers> AspNetApplicationUsers { get; set; }
public async Task<ClaimsIdentity> …Run Code Online (Sandbox Code Playgroud) 我试图在 3 次登录尝试失败 5 分钟后锁定用户登录。我已将这 3 行添加到App_Start/IdentityConfig.cs public static ApplicationUserManager Create( ... )方法中:
manager.MaxFailedAccessAttemptsBeforeLockout = 3;
manager.DefaultAccountLockoutTimeSpan = new TimeSpan(0, 5, 0);
manager.UserLockoutEnabledByDefault = true;
Run Code Online (Sandbox Code Playgroud)
之后,我通过POST /api/Account/Register(默认脚手架AccountController)注册新用户。帐户已创建并将LockoutEnabled属性设置为true。但是,如果我尝试POST /Token使用错误的密码登录几次,帐户不会被锁定。
我也很感兴趣/Token端点的实现在哪里。是在AccountController GET api/Account/ExternalLogin. 我已经在那里设置了断点,但是当我尝试登录时并没有在那里停止执行。
我错过了什么?
c# asp.net asp.net-identity asp.net-web-api2 asp.net-identity-2
我知道通过使用实体框架创建用户服务并创建密码哈希,我可以将一个或多个用户添加到网站!但即使我想注册批量用户(例如使用 excel 或 xml 文件上传用户列表),我也想使用 asp.net 身份功能。在我的场景中,我想通过上传 xml、json 或 excel 文件中的用户列表在网站上注册多个用户。我想在一笔交易中注册所有这些。 有谁知道吗?
我正在使用ASP.Net MVC和Identity 2.0开发多租户Web应用程序。我已经这样扩展了IdentityRole:
public class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string name) : base(name) { }
public string TenantId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是因为每个租户都有各自的角色集,例如“管理员”,“工作人员”等。
但是问题是,当我添加新角色时,如果“租户A”具有“管理员”角色,则当我向“租户B”添加“管理员”角色时,由于使用了“管理员”名称,因此我收到IdentityResult错误...这很明显,因为AspNetRoles表上的“名称”字段是唯一的...
IdentityResult roleResult = await RoleManager.CreateAsync(
new ApplicationRole
{
Name = "Admin",
TenantId = GetTenantId()
});
Run Code Online (Sandbox Code Playgroud)
但是,然后我如何自定义ASP.Net Identity,以便“ AspNetRoles”中的“名称”字段可以与“ TenantId”一起唯一,而不是单独?我发现了有关扩展IdentityRole的信息(就像我确实添加了一个字段一样),但没有有关更改或替换它的信息...