标签: asp.net-core-identity

使用ASP .NET Core Identity和EntityFrameworkCore注册新用户时出现InvalidOperationException

我正在关注使用Identity文档并尝试注册一个新用户(执行注册操作),但它失败并出现以下错误:

InvalidOperationException:无法为"ApplicationUser"创建DbSet,因为此类型未包含在上下文的模型中.

启动:

services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
    //password options
    options.Password.RequireDigit = false;
    // ...
})
Run Code Online (Sandbox Code Playgroud)

我使用的是标准的ApplicationUser:

public class ApplicationUser : IdentityUser
{
}
Run Code Online (Sandbox Code Playgroud)

在AccountController中注册操作:

public async Task<IActionResult> Register(RegisterViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = viewModel.UserName, Email = viewModel.Email };
        var result = await _userManager.CreateAsync(user, viewModel.Password); //<-- Exception happens here
        if (result.Succeeded)
        {
            await _signInManager.SignInAsync(user, isPersistent: false);
            _logger.LogInformation(3, "User created a new account with password.");
            return RedirectToAction(nameof(HomeController.Index), "Home");
        }

        string …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework asp.net-core-1.0 asp.net-core-identity

5
推荐指数
1
解决办法
6555
查看次数

实现SendEmailAsync接口并发送电子邮件

我不知道如何调用该SendEmailAsync函数

注册帖子页面

public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                // Send an email with this link
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, …
Run Code Online (Sandbox Code Playgroud)

c# email asp.net-core asp.net-core-identity

5
推荐指数
1
解决办法
2680
查看次数

IdentityServer4 - 会话管理和空闲超时或不活动时段

我已经配置了IdentityServer几个客户端,其中一些是使用Hybrid flow(桌面,iOS,并不重要)的本机应用程序.

我想强制注销超过X分钟不活动的用户,如果可能的话,再次重定向到登录页面.
我可以设法通过使用短暂刷新令牌以实现这一目标RefreshTokenExpiration = true,并且SlidingRefreshTokenLifetime = *DesiredTimeoutTime*,和每次调用API之前,客户端首先刷新用户的访问令牌.实际上,这会将会话管理空闲超时重载到刷新令牌的到期时间.
但这不是它的设计使用方式.刷新令牌应该是长寿的.
此外,对于API的每次调用,客户端都会刷新它的访问令牌,因为我使用持久存储来获取我的授权(特别是使用内置EF支持的SQL Server),这意味着我的方式比我更多曾经想过我的ID DB.

我指定我使用的是本机应用程序,因为我知道它(可能)可以使用cookie实现基于浏览器的应用程序,但同样,那些与本机应用程序不能很好地工作(对于我知道的所有内容,请放心如果我错了就纠正我.如果有可能以某种方式实现饼干 - 我很乐意听到.
总的来说,我真的缺乏对Identity.Application cookievs 的关系的了解Access Token.这有点令人困惑.

无论如何,我正在寻找更优雅的解决方案,如果有的话.
谢谢.

session asp.net-identity-3 asp.net-core identityserver4 asp.net-core-identity

5
推荐指数
0
解决办法
1327
查看次数

基于ASP.NET核心自定义策略的授权 - 不清楚

好的,ASP.NET核心中基于自定义策略的授权.我有点理解这个新的身份框架的想法,但仍然没有100%清楚你可以用这个来实现.假设我们在HomeController中有一个名为List的Action.此操作将查询并显示数据库中的产品列表.必须访问此列表的用户必须是Marketing部门的一部分.因此,在我们的政策中,我们检查用户是否有一个名为Division的声明,其值是Marketing.如果是,那么他将被允许查看列表,否则不会.我们可以像这样装饰我们的动作:

[Authorize(Policy = "ProductsAccess")]
public IActionResult List()  
{
    //do query and return the products view model
    return View();
}
Run Code Online (Sandbox Code Playgroud)

这一切都很好.它会很完美.

场景1:如果我想在产品级别添加策略,并且根据策略,用户将只看到其部门的产品,该怎么办?所以营销人员会看到他的产品,研发人员会看到他的等等.我怎样才能做到这一点?是否可以通过政策完成?如果有,怎么样?

场景2:在现场级访问怎么样?让我们说也许我想要隐藏某些字段?示例:所有产品都有某些必须对Managers可见且对其他用户隐藏的列?可以使用自定义策略完成吗?如果有,怎么样?

c# asp.net-core asp.net-core-identity

5
推荐指数
1
解决办法
561
查看次数

如果用户使用Google登录,则ASP.NET Core Identity 2.0 SignoutAsync不会注销用户

我有Asp.net核心身份版本2.0设置和运行.我发现,_signinManager.SignoutAsync一旦用户登录Google ,就无法注销用户.当我回到我的登录方法时,它只显示用户登录时其Claim对象仍然完好无损.

代码非常简单,如下所示

[AllowAnonymous]
public ActionResult TestGoogle()
{
    var redirectUrl = Url.Action(nameof(ExternalCallback), "Account", new { ReturnUrl = "" });
    var properties = _signInManager.ConfigureExternalAuthenticationProperties("Google", redirectUrl);
    return Challenge(properties, "Google");
}


public async Task<IActionResult> LogOff()
{
    await _signInManager.SignOutAsync();
    return RedirectToAction(nameof(HomeController.Index), "Home");
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core asp.net-core-identity asp.net-core-2.0

5
推荐指数
1
解决办法
6314
查看次数

AspNetCore 2.0身份-注入RoleManager的问题

我需要为ROLES创建CRUD操作。

我收到以下错误:

“无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager`的服务”

那么,我该如何注入roleManager?

我正在使用ASP Net Core 2.0 + Identity 2.2.1

类ApplicationUser

 public class ApplicationUser : IdentityUser
    {
        [Key]
        public override string Id { get; set; }
        public bool Type { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

现在在Startup.cs中

        services.AddIdentity<ApplicationUser, IdentityRole<int>>()
        .AddUserStore<UserStore<ApplicationUser, IdentityRole<int>, ApplicationDbContext, int>>()
        .AddRoleStore<RoleStore<IdentityRole<int>, ApplicationDbContext, int>>()
        .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

控制者

private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityUser> _roleManager;

public RolesController(UserManager<ApplicationUser> userManager, RoleManager<IdentityUser> roleManager)
{
    _userManager = userManager;
    _roleManager = roleManager; 
}


public IActionResult Index()
{
    return View(_roleManager.Roles);
}
Run Code Online (Sandbox Code Playgroud)

因此,我收到错误消息:“无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager`的服务。

c# asp.net-core asp.net-core-identity

5
推荐指数
1
解决办法
2359
查看次数

将现有的Microsoft.AspNet.Identity DB(EF 6)迁移到Microsoft.AspNetCore.Identity(EF Core)

我正在使用Microsoft.AspNet.Identity的应用程序(APS.net MVC)。现在,我想将应用程序更新为使用Microsoft.AspNetCore.Identity的 APS.net Core 。但是,这两种在每种模型中都有一些差异。是否有任何直接方法可以生成Microsoft.AspNetCore.Identit y相关更改的初始迁移,以便将现有数据库与Asp.net Core身份进行连接?

c# asp.net-mvc asp.net-identity asp.net-core asp.net-core-identity

5
推荐指数
1
解决办法
1385
查看次数

Asp.net核心身份成功登录重定向到登录页面

我有一个问题,在他们成功登录后,asp.net身份框架会将用户重定向回登录页面。

这是使用标准的Asp.net Core Identity。它是版本2.1.1。生成剃须刀页面的脚手架。不知道这是否有意义。

我知道用户已成功登录,因为我收到了日志消息

... Areas.Identity.Pages.Account.LoginModel:信息:用户已登录。

但随后,它将直接重定向回登录页面。

如果我使用提琴手,那么我可以看到请求上有一个cookie,因此从该角度来看,它看起来都很不错。

.AspNetCore.Identity.Application=CfDJ8KJxkuir9ZJIjFLCU2bzm9n6X...
Run Code Online (Sandbox Code Playgroud)

因此,我猜正在处理身份验证但不接受Cookie的中间件?

如果我可以看到auth的实际中间件在做什么,那么我可能有一个主意,但找不到。

任何帮助表示赞赏

c# asp.net asp.net-core asp.net-core-identity

5
推荐指数
2
解决办法
2999
查看次数

IdentityServer4签名密钥,验证密钥和.Net Core数据保护

Identity Server 4文档(此处为http://docs.identityserver.io/en/latest/topics/crypto.html?highlight=data%20protection)讨论了签名密钥和验证密钥。我知道签名密钥是使用

AddSigningCredential(<X509Certificate2>)
Run Code Online (Sandbox Code Playgroud)

有两个用于验证密钥的API

AddValidationKey(<X509Certificate2>)
AddValidationKeys(<Microsoft.IdentityModel.Tokens.AsymmetricSecurityKey[]>) 
Run Code Online (Sandbox Code Playgroud)

该文档讨论了对密钥过渡进行签名并将多个验证密钥添加到发现文档中。问题-

identityserver4 asp.net-core-identity asp.net-core-2.0

5
推荐指数
1
解决办法
2025
查看次数

Asp.net Core 2.1 Identity的LockoutEnabled属性实际上是什么?

我使用的是Asp.net Core 2.1 Identity,并且有一个名为“ LockoutEnabled ” 的属性,我认为此标志可以确定用户是否被锁定。但是医生说是

“指示用户是否可以被锁定的标志”

那么,此属性的作用是什么?锁定功能实际上是在此版本的库中实现的吗?谢谢。

asp.net-identity asp.net-core asp.net-core-identity

5
推荐指数
2
解决办法
2445
查看次数