Tag: asp.net identity

Store未实现IUserRoleStore ASP.NET Core 2.1 Identity

我正在使用ASP.NET Core 2.1 Identity。 我已经覆盖了IdentityUser,因为我需要在用户上添加一些额外的属性。 在Startup.cs中 services.AddDefaultIdentity().AddEntityFrameworkStores(); ApplicationDbContext.cs public partial class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } } PortalUser类 public class PortalUser : IdentityUser { [PersonalData] public DateTime? LastLoginDateUtc { get; set; } [PersonalData] public DateTime? RegistrationDateUtc { get; set; } } 这一切都很好。 我可以通过添加用户。 _userManager.CreateAsync(user) 但是,当我调用AddToRolesAsync向用户添加角色时,我遇到了exception。 有什么想法吗? _userManager.AddToRolesAsync(user, new List […]

充分利用具有n(3)层架构的MVC Owin身份

我一直在学习开箱即用的Owin Identity ,我喜欢它为我们提供用户管理的易用性。 然后我遇到的问题是它通过ApplicationDbContext直接与EF(貌似)进行交互,这是我不想要的。 我更喜欢使用我的3层架构,IE它与服务层(BLL)交互,后者与EF交互。 我找不到模板,教程,甚至起点来维护所提供的所有function并实现我想要的分离。 那么有没有办法在MVC Identity包中使用服务层代替ApplicationDbContext 。

在Identity 2.0中扩展IdentityUserRole

因此,我的系统要求角色具有相关的到期日期。 我已经实现了Identity 2.0框架,事情进展顺利,但我遇到了一个让我怀疑我的结构的问题。 public class ApplicationUserRole : IdentityUserRole { public override string UserId { get; set; } public override string RoleId { get; set; } public DateTime Expiry { get; set; } } public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base(“DefaultConnection”, throwIfV1Schema: false) { } static ApplicationDbContext() { Database.SetInitializer(null); } public static ApplicationDbContext […]

Owin Authentication.SignIn不工作

我尝试使用owin身份validation管理器来validation用户,但User.Identity.IsAuthenticated仍然是假的。 Startup.cs public partial class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR(); } } Startup.Auth.cs public partial class Startup { public static Func<UserManager> UserManagerFactory { get; set; } public Startup() { UserManagerFactory = () => { var userManager = new UserManager(new CustomUserStore()); return userManager; }; } public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { […]

Identity Owin是否需要LazyLoading?

tl; dr:Identity似乎要求禁用LazyLoading; 这是真的,什么是最干净的解决方法? 我使用EntityFramework 6.0.2,Identity EntityFramework 1.0.0和Identity Owin 1.0.0对一个简单的C#ASP.NET 4.5.1 MVC-5 Web应用程序进行了一些基本的AB测试,看来Owin要求延迟加载不是在ApplicationContext构造函数中禁用。 要复制该问题,只需使用Visual Studio 2013创建一个快速的MVC应用程序,使用MVC模板,将所有内容保留为默认值,但取消注释该行:’app.UseGoogleAuthentication();’ 在App_Start / Startup.Auth.cs中。 运行该应用程序并使用Google登录,完成它带您到的缩写注册页面并转到帐户/管理。 您应该会在底部看到2个Google按钮。 停止应用程序。 现在转到ApplicationContext.cs并更改构造函数,如下面的代码片段所示: public ApplicationContext() : base(“DefaultConnection”) { } //Works! public ApplicationContext() : base(“DefaultConnection”) { this.Configuration.LazyLoadingEnabled = false; } //Does not work 重试测试。 只能显示1个Google按钮。 如果LazyLoadingEnabled = false,则不会加载用户角色,登录(也可能是索赔)。 我的理论是,这是微软的监督/“未来特征”,因为Identity EntityFramework和Identity Owin都是版本1.0.0。 我的问题是,这个测试能否得到确认,最干净的工作是什么? 出于我的目的,我将使用.ToList()和其他方法在我想使用它时强制执行EagerLoading。 我并不真的需要禁用LazyLoading,如果你想总是使用预先加载,它只是一种更安全的代码编写方式。 即你错过了一个地方,它使它生产,你有一个很好的错误,在某些视图中你正在迭代模型和Model.xy y == […]

在ASP.NET中的IdentityUserRole 2.0中获取角色名称

在更新entity framework中的dll之前,我能够做到这一点 user.Roles.Where(r => r.Role.Name == “Admin”).FisrtOrDefault(); 现在,我只能做r.RoleId,我找不到一种方法来检索thar Role Id的名字。 我在我的控制器和AuthorizeAttribute类中使用它。 有人可以帮我吗? 问候

在覆盖成员时注册了抛出的inheritance安全规则

对于我的学校项目,我使用的是MVC项目附带的默认Account Controller注册函数: // POST: /Account/Register [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task Register(RegisterViewModel model){ if (ModelState.IsValid){ var user = new ApplicationUser(){UserName = model.UserName}; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded){ await SignInAsync(user, isPersistent: false); return RedirectToAction(“Index”, “Home”); } else{ AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); } […]

如何在我的自定义表而不是aspnet用户中使用哈希密码保存新记录?

我使用asp.net身份创建新用户但收到错误: 无法将值NULL插入列’Id’,表’Mydb.dbo.AspNetUsers’; 列不允许空值。 INSERT失败。\ r \ n语句已终止 但是在这里我没有像AspNetUsers这样的表,而是我拥有自己的用户表。 代码: Web.config:2个连接字符串 IdentityModel.cs: public class ApplicationUser : IdentityUser { public async Task GenerateUserIdentityAsync(UserManager manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType this.SecurityStamp = Guid.NewGuid().ToString(); var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return userIdentity; } public string Id […]

用于在MVC5中模拟ConfirmEmailAsync和其他UserManager方法的接口

我正在尝试对这种控制器方法进行unit testing,该方法在当前的MVC项目中开箱即用。 [AllowAnonymous] public async Task ConfirmEmail(string userId, string code) { if (userId == null || code == null) { return View(“Error”); } var result = await UserManager.ConfirmEmailAsync(userId, code); return View(result.Succeeded ? “ConfirmEmail” : “Error”); } AccountController有一个构造函数,它将ApplicationUserManager和ApplicationSignInManager作为参数,以及与私有setter匹配的属性用于测试。 但是,我无法弄清楚如何模拟ConfirmEmailAsync方法。 您可以在Identity命名空间中模拟各种接口: var store = new Mock<IUserStore>(); store.As<IUserEmailStore>() .Setup(x => x.FindByIdAsync(“username1”)) .ReturnsAsync((ApplicationUser)null); var mockManager = new ApplicationUserManager(store.Object); AccountController […]

轻松地为角色提供程序使用ASP.NET标识

我花了最近两天用我现有的数据库研究和实现新的ASP.NET身份系统。 更多相关内容: 将ASP.NET标识集成到现有的DbContext中 。 现在,我有一个工作的UserStore和RoleStore ,但我似乎无法弄清楚如何在我的ASP.NET MVC 5应用程序中利用它们而不编写像我所有混淆的所有Identity样本中的大量代码。 我想要实现两件事:1)使用cookie来维护授权; 2)使用角色来限制应用程序访问,无论是在视图中还是在控制器上呈现的内容。 为了能够使用我需要的那些显然使用Controller.User属性,它代表授权用户并查看它的角色。 如何让我的Identity实现实现这一目标? 最后,在身份样本中,我看到他们正在使用OWIN,我可以得到它,但它似乎是一种超级迂回的方式,我仍然没有得到如何正确实现。 至于索赔,他们让我误解了我的两倍。 我很欣赏任何指向正确的方向。