我使用asp.net核心默认网站模板和身份验证选择为个人用户帐户.如何创建角色并将其分配给用户,以便我可以使用控制器中的角色来过滤访问权限.
.Net Core 1.0.0 - SDK Preview 2(x64)
.Net Core 1.0.0 - VS"15"预览版2(x64)
.Net Core 1.0.0 - 运行时(x64)
因此,我们将RC1应用更新为上述最新版本.经过几个小时的切换参考,它正在运行.但是,登录时(AccountController/Login),我收到错误:
public class AccountController : BaseController
{
public UserManager<ApplicationUser> UserManager { get; private set; }
public SignInManager<ApplicationUser> SignInManager { get; private set; }
private readonly IEmailSender EmailSender;
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, IEmailSender emailSender)
{
UserManager = userManager;
SignInManager = signInManager;
EmailSender = emailSender;
}
// GET: /Account/Login
[HttpGet]
[AllowAnonymous]
public IActionResult Login(string returnUrl = null)
{
ViewBag.ReturnUrl = returnUrl;
return View();
} …
Run Code Online (Sandbox Code Playgroud) 我现在看到的ASP.NET Identity 3.0的所有示例都使用Entity Framework来存储与用户相关的数据.
是否有任何不使用Entity Framework的示例以及哪些ApplicationUser
类不是派生自Microsoft.AspNet.Identity.EntityFramework.IdentityUser
?
在ASP.NET Identity 2.x中,需要实现IUser
接口.现在似乎没有这样的界面 - 所以我们不确定如何User
正确定义类.几乎没有关于这个主题的文档.
第二个问题是AddIdentity
打电话Startup.ConfigureServices
.它与Microsoft.AspNet.Identity.EntityFramework
命名空间中的特定类非常相关,并且不清楚如何在没有这些类的情况下注册身份服务.
我正在尝试自定义ASP.NET Identity 3,以便它使用整数键:
public class ApplicationUserLogin : IdentityUserLogin<int> { }
public class ApplicationUserRole : IdentityUserRole<int> { }
public class ApplicationUserClaim : IdentityUserClaim<int> { }
public sealed class ApplicationRole : IdentityRole<int>
{
public ApplicationRole() { }
public ApplicationRole(string name) { Name = name; }
}
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, ApplicationDbContext, int>
{
public ApplicationUserStore(ApplicationDbContext context) : base(context) { }
}
public class ApplicationRoleStore : RoleStore<ApplicationRole, ApplicationDbContext, int>
{
public ApplicationRoleStore(ApplicationDbContext context) : base(context) { }
}
public class ApplicationUser …
Run Code Online (Sandbox Code Playgroud) asp.net type-constraints asp.net-identity asp.net-identity-3
当我查看ASP.NET 3 Identity时,它使用a string
而不是a Guid
用于唯一主键.
在我的Entity Framework
code first
用户ApplicationUser
类中,我继承了Identity类
public class ApplicationUser : IdentityUser
{
}
Run Code Online (Sandbox Code Playgroud)
这导致在我创建实体框架迁移时aspnetusers
,使用密钥类型nvarchar(450)
而不是创建的表创建uniqueidentifier
当我将它与ASP.NET Identity 2
ENtity Framework
项目进行比较时,它创建了一个ID字段uniqueidentifier
而不是nvarchar(450)
我会想象对于数据库性能主键和外键uniqueidentifier
会更好nvarchar(450)
有没有办法用于唯一键Guid
而不是用string
而uniqueidentifier
不是nvarchar(450)
用ASP.NET Identity 3
?
先前有一个问题如何将String转换为Guid,但我希望数据库表Id是Guid.
当长度为nvarchar(128)时,我发现了以前的BETA的另一个问题.给出的理由是并非所有数据库都支持Guids,并且它的灵活性也发生了变化.
是否必须有一种简单的方法从字符串更改为Guid而不重写整体identity 3
?
nvarchar(450)
在创建SQL Server数据库约束时,实际上是过度杀伤并提供各种警告.数据库管理员肯定不会喜欢这些警告.
c# asp.net-identity entity-framework-core asp.net-identity-3 asp.net-core
我正在使用ASP.NET Core Identity创建一个新用户,如下所示:
new User {
Email = "john@company.com",
Name = "John"
}
await userManager.CreateAsync(user, "password");
Run Code Online (Sandbox Code Playgroud)
我需要在创建用户时添加声明.我试过了:
new User {
Email = "john@company.com",
Name = "John",
Claims = new List<Claim> { /* Claims to be added */ }
}
Run Code Online (Sandbox Code Playgroud)
但Claim属性是只读的.
做这个的最好方式是什么?
我目前正在寻找一种解决方案,使用带有Identity 3的ASP .NET 5 MVC 6中的高级角色/组权限管理.我启动了一个带有集成的轻松登录系统的新预览启动Web项目.
现在我需要一个复杂的"用户权限管理",具有以下功能:
我已经看到这种身份已经广泛地为我提供了适合我的表结构.(例如AspNetUsers,AspNetUserRoles,AspNetRoles,AspNetRoleClaims),
但我错过了使用它们的好示例/文档.
对于MVC 5,我使用了这个示例:用户有许多组,一个组可以有多个角色(角色是类/函数源代码中的访问对象) ASP.NET Identity 2.0:实现基于组的权限管理
存在这些要求已经是一个工作示例,您不必重新发明轮子.
asp.net asp.net-mvc asp.net-core-mvc asp.net-identity-3 asp.net-core
我正在使用:
我正在寻找一些管理用户/角色的UI.
我试过的一些事情:
我需要用户使用开箱即用的身份验证登录到网站.我现在需要链接到Google(和其他服务)中的用户驱动器.我想使用ASP.Net身份的OAuth身份提供商来处理令牌交换,但我不希望它触及现有UserCredential或使用它SignIn
的UserPrincipal
我的目标是预防
AuthenticateCoreAsync
从返回AuthenticationTicket
导致修改到当前登录的用户身份题
设置自定义grantIdentity有什么影响IOwinContext.Authentication.SignIn()
?
SignInAsAuthenticationType能解决我的需求吗?
如果没有,何时使用?
使用Google提供商的理论代码
// The cookie needs to be First in the chain.
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "CustomExternal",
AuthenticationMode = AuthenticationMode.Passive,
CookieName = "MyAwesomeCookie",
ExpireTimeSpan = TimeSpan.FromMinutes(5),
//Additional custom cookie options....
});
//Note that SignInAsAuthenticationType == AuthenticationType
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions
{
AuthenticationType = "GoogleBackOffice",
ClientId = "abcdef...",
ClientSecret = "zyxwv....",
SignInAsAuthenticationType = "CustomExternal"
});
Run Code Online (Sandbox Code Playgroud) 我正在寻找一个变种
@if (SignInManager.IsSignedIn(User) && User.IsInRole(Roles.Administrator))
{
<div id="editArticle">
Run Code Online (Sandbox Code Playgroud)
但是不是在检查完政策之后检查角色,而是像在控制器中那样检查.
[Authorize(Policy = Policies.RequireAdmin)]
Run Code Online (Sandbox Code Playgroud)