标签: asp.net-identity-3

如何在DI中注册自定义UserStore和UserManager

这是我的设置:

public class ApplicationUser : IdentityUser<Guid>
{
}
public class ApplicationRole : IdentityRole<Guid>
{
}
public class ApplicationUserLogin : IdentityUserLogin<Guid>
{
}
public class ApplicationUserClaim : IdentityUserClaim<Guid>
{
}
public class ApplicationRoleClaim : IdentityRoleClaim<Guid>
{
}
Run Code Online (Sandbox Code Playgroud)

这是我的UserStore的定义

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, MyContext, Guid>
{
    public ApplicationUserStore(MyContext context, IdentityErrorDescriber describer = null)
        : base(context, describer)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的UserManager的定义

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor,
        IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators,
        IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer,
        IdentityErrorDescriber …
Run Code Online (Sandbox Code Playgroud)

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

13
推荐指数
2
解决办法
1万
查看次数

ASP.NET核心标识:没有角色管理器的服务

我有一个使用Identity的ASP.NET Core应用程序.它可以工作,但是当我尝试将自定义角色添加到数据库时,我遇到了问题.

在Startup中,ConfigureServices我添加了Identity和角色管理器作为范围服务,如下所示:

services.AddIdentity<Entities.DB.User, IdentityRole<int>>()
                .AddEntityFrameworkStores<MyDBContext, int>();

services.AddScoped<RoleManager<IdentityRole>>();
Run Code Online (Sandbox Code Playgroud)

在Startup中,Configure我注入RoleManager并将其传递给我的自定义类RolesData:

    public void Configure(
        IApplicationBuilder app, 
        IHostingEnvironment env, 
        ILoggerFactory loggerFactory,
        RoleManager<IdentityRole> roleManager
    )
    {

    app.UseIdentity();
    RolesData.SeedRoles(roleManager).Wait();
    app.UseMvc();
Run Code Online (Sandbox Code Playgroud)

这是RolesData班级:

public static class RolesData
{

    private static readonly string[] roles = new[] {
        "role1",
        "role2",
        "role3"
    };

    public static async Task SeedRoles(RoleManager<IdentityRole> roleManager)
    {

        foreach (var role in roles)
        {

            if (!await roleManager.RoleExistsAsync(role))
            {
                var create = await roleManager.CreateAsync(new IdentityRole(role));

                if (!create.Succeeded)
                {

                    throw new Exception("Failed …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-core-mvc asp.net-identity-3 asp.net-core

13
推荐指数
2
解决办法
2万
查看次数

ASP.NET Identity和IdentityServer有什么区别?

我无法理解两者之间的区别,asp.net identy基于OWIN,并且在IdentityServer介绍他时没有引入中间件,我很困惑..

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

13
推荐指数
1
解决办法
4150
查看次数

如何在Identity 3.0中获取当前的UserId?User.GetUserId返回null

我需要获得提出请求的用户ID.在以前的Identity版本中,我可以这样做:

User.Identity.GetUserId();
Run Code Online (Sandbox Code Playgroud)

但它似乎不再可用了.

还有类似的东西:

User.GetUserId();
Run Code Online (Sandbox Code Playgroud)

但它始终返回null,即使正确设置了User.Identity.IsAuthenticated并且正确设置了User.Identity.Name.

我应该用它做什么?

编辑:我的authenticaton逻辑基于[默认的Visual Studio 2015模板],到目前为止我的身份没有太大变化,所以如果你想检查它是如何完成的,你可以在我粘贴的github链接上看到它.

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

9
推荐指数
3
解决办法
8771
查看次数

在Identity 3中创建声明标识

Visual Studio 2015脚手架使用UserManager<TUser>不能用于创建的脚手架ClaimsIdentity.有没有人有一个如何做到这一点的工作示例?

VS2015脚手架抛出错误:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
    // Note the authenticationType must match the one 
    // defined in CookieAuthenticationOptions.AuthenticationType
    var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

    // Add custom user claims here
    return userIdentity;
}
Run Code Online (Sandbox Code Playgroud)

注意:我添加了ApplicationUser与之不冲突的属性IdentyUser.

razor entity-framework-6 asp.net-identity-3 asp.net-core

9
推荐指数
1
解决办法
1515
查看次数

在ASP.NET One Core中更新声明值

我在MVC 6(Asp.Net One Core)中有一个Web应用程序,我正在使用基于声明的身份验证.在Login方法中,我设置了声明:

var claims = new Claim[]
{
    new Claim("Name", content.Name),
    new Claim("Email", content.Email),
    new Claim("RoleId", content.RoleId.ToString()),
};

var ci = new ClaimsIdentity(claims, "password");
await HttpContext.Authentication.SignInAsync("Cookies", new ClaimsPrincipal(ci));
Run Code Online (Sandbox Code Playgroud)

现在,如果用户例如更改了用户配置文件中的电子邮件,我该如何更改"电子邮件"声明的电子邮件值?为了更新cookie,我必须再次使用SignOutAsync和SignInAsync吗?最好的解决方案是将其保存到经典会话中吗?还有更好的解决方案吗?我完全错了?

有什么建议?

c# asp.net asp.net-mvc claims-based-identity asp.net-identity-3

9
推荐指数
2
解决办法
7132
查看次数

ASP.NET Identity(使用IdentityServer4)获取外部资源oauth访问令牌

我已经浏览了identityServer4的文档,并且我将其设置为使用Microsoft Office 365作为登录提供程序.当用户登录后我想创建一个按钮,他可以让我的应用程序使用graph.microsoft.com的webhooks api订阅他的日历事件

startup.cs中的代码

app.UseMicrosoftAccountAuthentication(new MicrosoftAccountOptions
{
     AuthenticationScheme = "Microsoft",
     DisplayName = "Microsoft",
     SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,

     ClientId = "CLIENT ID",
     ClientSecret = "CLIENT SECRET",
     CallbackPath = new PathString("/signin-microsoft"),
     Events = new OAuthEvents
     {
         OnCreatingTicket = context =>
         {
             redisCache.Set("AccessToken", context.AccessToken.GetBytes(), new DistributedCacheEntryOptions
             {
                 AbsoluteExpiration = DateTimeOffset.UtcNow.AddDays(3)
             });
             return Task.FromResult(context);
         }
     }
     Scope =
     {
         "Calendars.Read",
         "Calendars.Read.Shared",
     },
     SaveTokens = true
});
Run Code Online (Sandbox Code Playgroud)

但这显然不是一条可行的道路.我只是为了测试目的而做了这个,并制作了所需订阅的PoC.

现在我想知道是否有更聪明的方式与identityServer通信,允许我获得这个外部访问令牌,以便我可以代表我的登录用户使用microsoft api?

或者,我唯一的选择是直接从此OAuthEvent获取Microsoft AccessToken并将其直接存储在链接到登录用户的数据库中?

我真的需要这个,因为我的大部分功能都是基于来自第三方的数据.

c# oauth asp.net-identity asp.net-identity-3 identityserver4

8
推荐指数
1
解决办法
4766
查看次数

ASP.Net核心SignInManager lockoutOnFailure

ASP.Net Core具有处理用户身份验证的SignInManager.其中一种方法是PasswordSignInAsync(string username, string password, bool isPersistent, bool lockoutOnFailure).将lockoutOnFailure设置为true应该在一定次数的登录尝试失败后暂时锁定用户.

查看数据库中的AspNetUsers表,我看到以下内容:

  • 每次失败访问时AccessFailedCount增加1,当它达到5时,它会翻转为0.
  • 滚动到0时LockoutTimeEnd设置为未来5分钟.
  • 但是,即使在翻转后,LockoutEnabled仍然为0,用户可以继续尝试登录.

看起来预期的功能是允许5次登录尝试,然后将帐户锁定5分钟.

所以我的问题是:

  1. 如何设置允许的失败登录次数?
  2. 如何设置锁定期?
  3. 锁定为什么不触发?

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

7
推荐指数
1
解决办法
3521
查看次数

ASP.NET Core更改AccessDenied路由

我在路由AccessDenied时遇到了一些麻烦,可能还有Login/Logout路径.该项目是一个剥离的默认项目,没有更多的魔力.因此存在一个AccountAccessDenied()方法的控制器.

我现在正在尝试的是(这是互联网商品提供的解决方案)

services.Configure<CookieAuthenticationOptions>(options =>
{
    options.LoginPath = new PathString("/");
    options.AccessDeniedPath = new PathString("/InactiveSponsor");
    options.LogoutPath = new PathString("/");
});
Run Code Online (Sandbox Code Playgroud)

但这绝对没有区别.那么任何想法?任何想法为什么不工作以及如何使其工作.

这是我的Startup.cs

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();

    if (env.IsDevelopment())
    {
        // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
        builder.AddApplicationInsightsSettings(developerMode: true);
    }
    Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; }

// This method gets …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc-routing asp.net-identity-3 asp.net-core

7
推荐指数
3
解决办法
7505
查看次数

Asp.Net核心MVC6如何在Identity 3中初始添加角色

我在Stackoverflow中已经找到了这个,到目前为止看起来有很多关于在Identity 12中添加角色的问题,但它在Identity 3中有所不同.

我想在数据库中播种角色.我只有两个.我打算使用我注入到类中的_roleManager.没关系.我的问题是..似乎没有任何实际添加角色的方法.使用CreateAsync是为了将用户添加到角色..你如何编码使用"_userManager"添加角色或者你必须这样做其他方式?

asp.net-mvc asp.net-identity-3 asp.net-core

6
推荐指数
1
解决办法
1万
查看次数