标签: asp.net-core-identity

将标识移动到类库ASP.Net Core

我开发了一个基于ASP.Net核心的新项目.我已将所有EF代码(模型,映射,DbContext)移动到专用DAL类库中,以遵循SOLID规则的单一责任原则.

但是,我现在需要在我的项目中添加身份验证,并且需要将以下内容添加到我的Web项目的Startup.cs中,如不同的教程所示:

services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

它将涉及添加Microsoft.AspNetCore.Identity.EntityFrameworkCore包,在我看来,我开始通过将此包包含到我的Web项目中来打破SRP规则.

是否可以将所有身份代码(服务,模型)作为外部类库移动,就像我为DAL所做的那样.

entity-framework-core asp.net-core asp.net-core-identity

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

如何确认ASP.Net Core 1.1MVC中的电话号码

我无法弄清楚如何在asp.net核心1.1中进行电话号码确认

身份服务配置包含要求确认的电子邮件和/或电话号码的明确选项.

它可以通过以下方式完成:

services
    .AddIdentity<User, Role>(options =>
    {
        options.SignIn.RequireConfirmedEmail = true;
        options.SignIn.RequireConfirmedPhoneNumber = true;
     });
Run Code Online (Sandbox Code Playgroud)

由于UserManager包含显式令牌生成器及其验证器,因此电子邮件的验证非常简单:

var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
Run Code Online (Sandbox Code Playgroud)

生成的令牌可以通过以下方式验证:

var result = await _userManager.ConfirmEmailAsync(user, code);
Run Code Online (Sandbox Code Playgroud)

如果令牌有效,上面的行将把user.EmailConfirmed标志切换为true.

现在的问题是我没有看到类似的方法来生成电话验证令牌及其等效方法来验证它(如果成功,它应该将user.PhoneNumberConfirmed标志切换为true).

但是,用户管理器包含的用户手机更改方法很少:

_userManager.GenerateChangePhoneNumberTokenAsync();
Run Code Online (Sandbox Code Playgroud)

_userManager.VerifyChangePhoneNumberTokenAsync();
Run Code Online (Sandbox Code Playgroud)

但似乎这些方法不会切换user.PhoneNumberConfirmed标志.

我错过了什么吗?确认用户电话号码的正确方法是什么(换句话说,将user.PhoneNumberConfirmed设置为true)?

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

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

在Asp.Net Core Identity中要求唯一的电话号码

在Asp.Net Core Identity框架中,通过设置RequireUniqueEmail = true,我可以轻松地要求一个唯一的电子邮件地址。

有什么办法可以对用户的电话号码执行相同的操作?请注意,我不想要求输入经过确认的电话号码。不需要用户输入电话号码,但是如果输入,则它必须是唯一的。

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

6
推荐指数
2
解决办法
1162
查看次数

IdentityServer4对基于角色的Web API授权

我使用IdentityServer4(v2.2.1)与.Net Core 2.0和Asp.Net核心身份.我的解决方案中有三个项目.

  1. IdentityServer
  2. Web API
  3. MVC Web应用程序

我正在尝试在我的Web API上实现基于角色的授权,以便任何客户端都可以将访问令牌传递给Web API以访问资源.

目前我可以在MVC应用程序控制器上实现Roles基础授权,但我无法为WEB API Controller传递/配置相同的权限.

以下是Identity Server Files:Config.cs

public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>
        {
            //SCOPE - Resource to be protected by IDS 
            new ApiResource("TCSAPI", "TCS API")
            {
                UserClaims = { "role" }
            }
        };
    }


 public static IEnumerable<Client> GetClients()
    {
        return new List<Client>
        {
new Client
            {
                ClientId = "TCSIdentity",
                ClientName = "TCS Mvc Client Application .",
                AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
                RequireConsent = false,
                ClientSecrets =
                {
                    new Secret("secret".Sha256()) …
Run Code Online (Sandbox Code Playgroud)

identityserver4 asp.net-core-identity

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

如何在没有角色的情况下使用 ASP.NET Core Identity?

在没有角色实现的情况下在asp.net core 2中实现身份是否可行?
我试图实现以下内容:

services.AddIdentityCore<TUser>();
Run Code Online (Sandbox Code Playgroud)

但这似乎也不起作用。

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

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

无法解析 Microsoft.AspNetCore.Identity.RoleManager for ASP.Net Core 2.0 类型的服务

如何在 ASP.Net Core 2.0 中注入 RoleManager?

我收到以下错误:

无法解析 Microsoft.AspNetCore.Identity.RoleManager for ASP.Net Core 2.0 类型的服务

我有以下代码:

  // add identity
  var builder = services.AddIdentityCore<AppUser>(o =>
  {
    // configure identity options
    o.Password.RequireDigit = false;
    o.Password.RequireLowercase = false;
    o.Password.RequireUppercase = false;
    o.Password.RequireNonAlphanumeric = false;
    o.Password.RequiredLength = 6;
  });
  builder = new IdentityBuilder(builder.UserType, typeof(AppRole), builder.Services);
  builder.AddSignInManager<SignInManager<AppUser>>();
Run Code Online (Sandbox Code Playgroud)

AppUser 和 AppRole 分别派生自 IdentityUser 和 IdentityRole。

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

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

每个租户的唯一用户名和电子邮件

我正在使用ASP.NET Core 2.1.

我想覆盖默认的用户创建相关验证机制。

目前我无法使用相同的UserName.

我的ApplicationUser模型有一个名为TenantID.

我想要实现的目标:UserName&EmailAddress必须是每个租户唯一的。

我一直在谷歌上搜索一个解决方案,但没有找到太多asp.net core关于这个的信息。

大多数结果只会涵盖Entity Framework方面,就好像它只是一个覆盖 OnModelCreating(...)方法的问题。有些与 ASP.NET Identity 的非核心版本有关。

我想知道我是否应该继续调查OnModelCreating方法?

或者,还有其他需要覆盖的东西Identity

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

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

调试 ASP.Net Core 2.1 时自动登录

我正在尝试在我构建的 ASP.net core 2.1 应用程序上自动登录以进行调试。

得到错误:

HttpContext 不能为空。

下面的代码位于Startup.cs文件中

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider ServiceProvider)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        app.UseCookiePolicy();

        CreateRoles(ServiceProvider).Wait();

        if (env.IsDevelopment())
        {
            DeveloperLogin(ServiceProvider).Wait();
        }
    }


    private async Task DeveloperLogin(IServiceProvider serviceProvider){

        var UserManager = serviceProvider.GetRequiredService<UserManager<User>>();
        var signInManager = serviceProvider.GetRequiredService<SignInManager<User>>();

        var …
Run Code Online (Sandbox Code Playgroud)

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

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

共享服务器上 PersistKeysToFileSystem 的文件路径

我正在尝试让我的密钥对登录的用户保持不变。由于我目前正在为网站使用共享主机,因此我决定使用文件系统来存储密钥环。所以代码看起来像这样:

services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(""))
.SetApplicationName("MyWebsite")
.SetDefaultKeyLifetime(TimeSpan.FromDays(90))
.ProtectKeysWithCertificate(cert);
Run Code Online (Sandbox Code Playgroud)

然而,我真正不明白的是我应该把这些钥匙放在哪里,以及我要经过什么路径才能让它们在那里。由于这是一个 MVC 核心应用程序,我有点困惑,在 MVC 5 中我会将它放在 App_Data 文件夹中,但这里没有 App_Data 文件夹,我想确保它保持安全并且无法通过浏览器访问。

另一件事是我传递的是相对路径还是直接路径?如果是相对的,我的起点在哪里?它是 bin、根目录还是其他什么?

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

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

User.Identity.Name 在 PasswordSignInAsync MVC .Net Core 3.0 之后始终为 null 并声明计数为 0

我有一个无法使用 ASP.NET MVC Core 3.0 解决的问题。登录后,结果成功并成功返回到我想要登录的页面,当我检查cookie或会话时,我可以看到API成功添加了它们。但是当我尝试获取 User.Identity.Name 时,它​​始终为 null,并且 isAuthenticated 始终等于 false。这就像 app.UseAuthentication() 不读取 cookie 或会话。

我的启动.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContextPool<AnisubsDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("AnisubsDBConnection")));

        services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<AnisubsDbContext>()
            .AddDefaultTokenProviders();
        services.AddMvc();
        services.AddControllersWithViews();


        services.AddAuthentication(options =>
        {
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
            .AddFacebook(facebookOptions …
Run Code Online (Sandbox Code Playgroud)

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

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