标签: asp.net-core-identity

JS 获取 cookie ASP.NET Core Identity

对于使用 Identity 的 ASP.NET Core 项目,我有一些 cookie。在客户端,我想检查用户是否登录。我试图获取.AspNetCore.Identity.Applicationcookie,但 js 中没有这样的 cookie document.cookie。我可以从js获取ASP.NET Core Identity cookie吗?或者检查用户是否使用 cookie 登录的最佳方法是什么?

javascript cookies asp.net-core asp.net-core-identity

4
推荐指数
1
解决办法
4331
查看次数

Asp.NetCore Identity - 创建自定义令牌提供程序

所以我尝试使用创建自定义令牌UserManager<TUser>.GenerateUserTokenAsync(TUser user, string tokenProvider, string purpose)方法创建自定义令牌,但当我尝试生成令牌时不断收到错误。

我知道目的可以只是纯文本,声明令牌的用途,并且在稍后验证令牌时需要相同。所以我的猜测是tokenProvider就是问题所在。

经过一些研究后,我得出的结论是我需要tokenProviderStartup.cs,但我发现的信息似乎对我不起作用。

任何人都可以在这里提供一些有关如何实施的见解吗custom tokenProvider?如果我的研究是错误的,我该如何使其发挥作用?

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

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

IdentityUser如何验证旧密码?

我希望只有当他/她输入正确的旧密码时才能更改当前用户密码,如下所示:

更新用户密码的方式

但我一直在努力在这里和其他地方找到一个优雅的解决方案,
当前的解决方案如下所示:

var oldPasswordHashed = _userManager.PasswordHasher.HashPassword(appUser, model.OldPassword);
if (oldPasswordHashed == appUser.PasswordHash)
{
    var result = await _userManager.ChangePasswordAsync(appUser, appUser.PasswordHash, model.NewPassword);
    if (!result.Succeeded)
    {
        ModelState.AddModelError(nameof(EditUserViewModel.OldPassword), "Error at changing password, retry later.");
        return View(model);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

4
推荐指数
1
解决办法
1729
查看次数

.net core Identity,获取特定的注册错误条件

我希望测试/处理从 UserManager 返回的特定错误条件,例如:由于用户名已存在文件等而导致注册失败

var user = new SiteUser() { UserName = username, Email = RegisterViewModel.Email };
var result = await _userManager.CreateAsync(user, RegisterViewModel.Password);
if (!result.Succeeded)
{
    // here I want to test for specific error conditions
    // eg: username already on file, etc
    // how can I do this?
}
Run Code Online (Sandbox Code Playgroud)

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

4
推荐指数
1
解决办法
588
查看次数

Question about ASP.NET Core 3 Identity / Identity Server / SPA support for Resource Owner Password Grant Type

As per Authentication and authorization for SPAs, I have created a new SPA with support for API authorization. You can view this on GitHub.

In order to support integration tests, I have added a new client (see appsettings.json) that is allowed the resource owner password grant type:

"SecureSpa.IntegrationTests": {
  "Profile": "IdentityServerSPA",
  "AllowedGrantTypes": [ "password" ],
  "ClientSecrets": [ { "Value": "K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=" } ],
  "AllowedScopes": [ "SecureSpaAPI", "openid", "profile" ]
}
Run Code Online (Sandbox Code Playgroud)

Then within WeatherForecastControllerTests.cs, I attempt to request …

asp.net-core identityserver4 asp.net-core-identity asp.net-core-3.0

4
推荐指数
1
解决办法
1537
查看次数

Asp.Net Core 3 Identity - 来自浏览器的 JWT 中不存在自定义声明

Asp.Net Core 3.0
我正在使用ASP.NET Core web application with Angular and Authentication(个人用户帐户)模板(来自 Visual Studio 2019)。
我的目的是在生成的内容中添加一些自定义声明JWT并在浏览器中使用它们。
为了做到这一点,我延长了UserClaimsPrincipalFactory

public class MyCustomClaimsInjector : UserClaimsPrincipalFactory<ApplicationUser>
{
    public MyCustomClaimsFactory(UserManager<ApplicationUser> userManager, IOptions<IdentityOptions> optionsAccessor) : base(userManager, optionsAccessor)
    {
    }

    protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
    {
        var id = await base.GenerateClaimsAsync(user);
        id.AddClaim(new Claim("my_claim1", "AdditionalClaim1"));
        id.AddClaim(new Claim("my_claim2", "AdditionalClaim2"));
        return id;
    }
}
Run Code Online (Sandbox Code Playgroud)

同样,我已经在 Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddClaimsPrincipalFactory<MyCustomClaimsFactory>();


        services.AddIdentityServer()
            .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

        services.AddAuthentication()
            .AddIdentityServerJwt();
        services.AddControllersWithViews();
        services.AddRazorPages();
        // …
Run Code Online (Sandbox Code Playgroud)

c# authentication jwt asp.net-core-identity asp.net-core-3.0

4
推荐指数
1
解决办法
2889
查看次数

Store 没有实现 IUserRoleStore&lt;TUser&gt; UserManager&lt;TUser&gt;.GetUserRoleStore() ASP.NET Core MVC 3

我已经实现了IdentityAsp.Net.Core MVC 3因为我们已经有了要使用的数据库表。调用下面的方法时出现错误,并且它们都发生在UserManager对方法的同一个调用中。查看此方法的源代码是因为 Store 变量为空。但我不太确定如何确保它不为空。我在网上查看了各种解决方案,但没有一个能解决我的问题。

我的解决方案正在使用SignInManagerfor 密码,但我无法让它通过 Roles 的错误。

配置服务方法

public void ConfigureServices(IServiceCollection services)
{           
    services.AddControllersWithViews();
    services.AddMvc(options =>
    {
        var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
        options.Filters.Add(new AuthorizeFilter(policy));
        options.EnableEndpointRouting = false;
    });
    services.AddDbContext<EntitiesModel>(options => options.UseSqlServer(
            Configuration["Data:ConnectionStrings:XXXXXXXXXXXXX"]));
    services.AddIdentity<UserViewModel, UserRoleViewModel>().AddDefaultTokenProviders();
    services.AddTransient<IUserStore<UserViewModel>, UserStore>();
    services.AddTransient<IRoleStore<UserRoleViewModel>, RoleStore>();
    services.ConfigureApplicationCookie(options =>
    {
        options.Cookie.HttpOnly = true;             
        options.LoginPath = "/Login";
        options.LogoutPath = "/Logout";
    });         
}
Run Code Online (Sandbox Code Playgroud)

用户存储

public class UserStore : IUserStore<UserViewModel>, IUserPasswordStore<UserViewModel>, IUserEmailStore<UserViewModel>, IUserRoleStore<UserRoleViewModel>
{ 
....
Run Code Online (Sandbox Code Playgroud)

RoleStore

public class RoleStore …
Run Code Online (Sandbox Code Playgroud)

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

4
推荐指数
1
解决办法
1363
查看次数

当可以从用户对象中提取所需的属性时,像 GetPhoneNumberAsync(IdentityUser user) 这样的 UserManager 方法的目的是什么?

我试图了解 ASP.Net Core Identity UI 视图背后的一些逻辑。

例如,Account\Manage\Index.cshtml.cs 包含以下代码:

    private async Task LoadAsync(IdentityUser user)
    {
        var userName = await _userManager.GetUserNameAsync(user);
        var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

        Username = userName;

        Input = new InputModel
        {
            PhoneNumber = phoneNumber
        };
    }
Run Code Online (Sandbox Code Playgroud)

当我们可以直接从用户对象中提取 PhoneNumber 属性时,为什么要调用 _userManager.GetPhoneNumberAsync(user) ?

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

4
推荐指数
1
解决办法
303
查看次数

PersonalDataAttribute 有什么用?

我只是偶然发现 ASP.NET Core Identity 框架提供了PersonalData 属性。文档只是说:

用于表示某物被视为个人数据。

好的。这是什么意思?它对身份框架的工作方式或作用有什么影响吗?或者它纯粹是装饰性的,以便我可以对某些对象进行一些反思并记录我的代码?

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

4
推荐指数
1
解决办法
983
查看次数

具有一对多关系的自定义身份用户类在数据库中创建额外的列

ASP.NET Core IdentityEntity Framework Coreon一起使用.NET 5,使用带有 postgresql 数据库的代码优先方法。

我正在尝试像这样扩展身份类

public class User : IdentityUser<int>
{
    public ICollection<UserLogin> Logins { get; set; }
}

public class UserLogin : IdentityUserLogin<int>
{
    public User User { get; set; }
}

public sealed class AppDbContext : IdentityDbContext<User, Role, int, UserClaim,UserRole, UserLogin, RoleClaim, UserToken>
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行迁移时,它生成的表看起来像这样 -

在此处输入图片说明

请注意,UserId1已经创建了一个额外的列。

我希望它能够识别UserLogin.User导航Id应该与IdentityUserLogin.UserId属性相对应(根据 MS 在此处制定的约定:https : …

c# entity-framework-core asp.net-core-identity entity-framework-core-migrations .net-5

4
推荐指数
1
解决办法
77
查看次数