我正在启动一个新的 .Net Core Web 应用程序。我想使用数据库中已存在的身份验证表(aspnet_Users、aspnet_Membership 等)。但我不知道该怎么做。
创建项目后,我选择“个人用户帐户”选项进行身份验证。然后我需要在“在应用程序内存储用户帐户”和“连接到云中的现有商店”之间进行选择。
似乎没有一个适合我的需要。
authentication asp.net-membership asp.net-core asp.net-core-identity
我正在考虑向使用 ASP.NET Core Identity 和用户本地 SQL 数据库的现有 ASP.NET Core 应用程序添加 OpenID Connect 支持。这些用户在应用程序中拥有配置文件,并且有多个链接到他们的数据库表,例如他们创建的事件和项目。
我的问题是,是否有推荐的方法来确保当用户使用 OpenID Connect 通过 Azure AD 进行身份验证时,他们会以某种方式与 AspNetUsers 数据库表中的现有用户关联?
c# asp.net azure-active-directory openid-connect asp.net-core-identity
我正在使用 .NET 5、ASP.NET Core 5 Web API 和 ASP.NET Core Identity。我有一个 JWT 令牌:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJFbWFpbCI6ImR1Y3R4QG1wc29sdXRpb25zLmlvIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZWlkZW50aWZpZXIiOiJiM2E2ZTI5Zi1hZjdjLTRhZmUtYjA4Ni1jYmVjMjAzOTYxYmUiLCJleHAiOjE2MDE4ODk4OTksImlzcyI6Imh0dHA6Ly9tcHNvbHV0aW9ucy5pbyIsImF1ZCI6Imh0dHA6Ly9tcHNvbHV0aW9ucy5pbyJ9.0HmUptS8_VMPuYXMlSwfafhusiYPfyOPPaiSo6uSOMs
Run Code Online (Sandbox Code Playgroud)
我需要获取电子邮件字符串foo@example.com。如何在 C# 中解码 JWT 令牌,然后获取电子邮件字符串?
我正在 .NET 5.0 中使用 EF Core 5 和 Identity 进行身份验证/授权来开发 ASP.NET Core Razor Pages 应用程序,并且我尝试使用“干净的架构”来实现此目的。
该应用程序实现了 3 种“类型”的用户:主管、所有者和员工。每个都是单独的实体,因此保留在自己的数据库表中。每个用户类型都有进一步的相关表,此处未显示。
ApplicationUser(主要实体,存储在AspNetUsers表中)需要与每个用户“类型”(依赖实体)建立一对零或一的关系:
//
// Infrastructure
//
namespace MyApp.Identity
{
public class ApplicationUser : IdentityUser<Guid>
{
...
}
}
//
// Core
//
namespace MyApp.Domain.Entities
{
public class Supervisor
{
public int Id { get; set; } // PK
public string Department { get; set; }
public Guid UserId { get; set; } // FK
...
}
public class Owner
{ …Run Code Online (Sandbox Code Playgroud) asp.net entity-framework-core clean-architecture asp.net-core-identity
TL; DR - 标题:)
背景故事:
我正在构建一组应用程序(包括RESTful API和几个客户端 - 纯Web应用程序和移动应用程序).所有的核心API都将被写入ASP.NET Core.我想解决身份验证授权问题.
我不想使用云或外部服务,因为保持所有用户相关数据关闭是我的首要任务(包括密码).
根据我的阅读,似乎ASP.NET Core Identity提供了与所需的身份验证和授权相关的所有功能和流程.我可以管理整个用户群,包括散列密码,以及我自己与用户相关的所有数据.它是抽象和混凝土的集合(如果需要,我可以扩展).
我还读到我可以整合IdentityServer 4.从我的理解,它给了我额外的安全层,我可以将其用作安全令牌服务(STS),并为我提供OAuth 2.0授权和OpenID身份验证的实现,这很棒,但是...是吗?我只是想了解使用它的好处是什么.
我的问题有两个部分:
以下问题有一个很好的答案,它回答了我的很多问题,但我仍然想要一个特定于我的问题的答案.
提前致谢!
authentication oauth-2.0 asp.net-core identityserver4 asp.net-core-identity
是否有可能不使用电子邮件注册并登录asp.net核心身份?
用户只需通过手机号码注册并通过短信验证登录.
asp.net-mvc asp.net-identity asp.net-core asp.net-core-identity
我正在尝试UserManager从原始内容创建自己的扩展,并且当我通过电子邮件进行搜索时,找不到用户。但是,如果我从上下文进行搜索,那么我是否找到了用户(请参见Get方法)。为了验证它是否确实实现良好,我重写了该FindByEmailAsync方法并确实对其进行了调用,但是我不知道为什么用户找不到它。一些帮助?谢谢!
public void ConfigureServices(IServiceCollection servicesCollection)
{
servicesCollection.AddDbContext<MyIndentityContext>(currentOptions =>
currentOptions.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
servicesCollection.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<MyIndentityContext>()
.AddRoleStore<ApplicationRoleStore>()
.AddUserStore<ApplicationUserStore>()
.AddUserManager<ApplicationUserManager>()
.AddRoleManager<ApplicationRoleManager>()
.AddSignInManager<ApplicationSignInManager>()
.AddDefaultTokenProviders();
...
...
...
}
public class MyIndentityContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
{
private readonly IConfiguration _configuration;
private readonly IHttpContextAccessor _httpContextAccessor;
public MyIndentityContext(DbContextOptions dbContextOptions, IHttpContextAccessor httpContextAccessor,
IConfiguration configuration)
: base(dbContextOptions)
{
_configuration = configuration;
_httpContextAccessor = httpContextAccessor;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("Sample.API");
}
}
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
public ApplicationRoleManager(IRoleStore<ApplicationRole> roleStore, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Asp.Net Core Identity 创建基本的 IdentityServer4,如本教程中所示。
但是当我调用登录方法时:
return Challenge(new AuthenticationProperties {
RedirectUri = "/Home/Index"
}, "oidc");
我收到 404 错误:
http://localhost:5000/account/login?returnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3Dmvc%26redirect_uri%3Dhttps%253A%252F%252Flocalhost%253A44391%252Fsignin-oidc%26response_type%3Dcode%2520id_token%26scope%3Dopenid%2520profile%2520api1%26response_mode%3Dform_post%26nonce%3D636682993147514721.ZDA2MmI5ZTgtMWU3Yi00ZjMzLTkyODMtZjBiNWIzMjUzZTRjZmYxMjIxYWItOTk5NS00OGJlLWE0M2EtOTg3ZTYyM2EzZWVk%26state%3DCfDJ8D1ISazXjTpLmRDTOwhIeT5cVwo-oh7P4hDeZa0Q7cSfU6vKRDTEu3RHraTyz4Wb8oQngGo-qAkzinXV2yFJuqClVRB_1gwLLXIvVK4moxtgGjZUGUJIDmoqQHrQCOxGNJLrGkaBiS74vxd1el8N1wSseoSBlqZD94OlShI53wgPNKXPiDzT0FLOI47MNwHwzW0d5q0n752kZiVp2V31CZemI6wtaEgte3Mb9iouFzrSyAW5XaBMdDEnAGPCNZ2d5Zfgwb2Cmp61B-I9t05aDHqR-5cxYtr0PVVM6PwBKy-1olSFH8uIc8ku0UJn7PY0WA%26x-client-SKU%3DID_NETSTANDARD1_4%26x-client-ver%3D5.2.0.0
我需要任何额外的视图和控制器吗?我认为将使用 Asp.Net Core Identity 中的内容。
我的 IdentityServer4 配置:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddDefaultUI()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// configure identity server with in-memory stores, keys, clients …Run Code Online (Sandbox Code Playgroud) c# asp.net-core identityserver4 asp.net-core-identity asp.net-core-mvc-2.1
我有一个 .net 核心应用程序,我在注册后制作了一个电子邮件令牌,然后通过电子邮件发送。这个想法是,用户可以在客户端应用程序中使用该电子邮件令牌,我想检查它的有效性(如果它已过期,如果它是与给定电子邮件关联的正确令牌)。
我试图在 userManager 中找到我可以使用的任何方法。我发现的是,VerifyUserTokenAsync(ApplicationUser user, string tokenProvider, string purpose, string token)但我不知道要传递什么参数。
因此,与任何人都可以帮助tokenProvider和purpose
我想提一提的是,电子邮件令牌与生成GenerateEmailConfirmationTokenAsync。我可以使用 来检查令牌ConfirmEmailAsync,如果结果不成功,则令牌无效,但如果令牌有效,我不想将 EmailConfirmed 设置为 true。
我有几种不同类型的用户(InternalUser、Clinician和Patient)可以登录我的网站,因此,对于每种类型的用户,我创建了一个从ApplicationUser.
如果登录用户访问网站的主页 ( https://example.com/ ),那么 MVC 控制器代码需要确定它是哪种类型的用户,以便它可以将重定向返回到正确的“主”页面(https://example.com/clinician、https://example.com/patient等)。
在我看来,使用声明作为一种简单、低成本的方式来区分控制器中的用户类型似乎是合理的,而不必从数据库加载当前用户。当我注册用户时,我添加了一个代表用户“类型”的声明:
await _userManager.AddClaimAsync("ihi:user_type", "clinician");
Run Code Online (Sandbox Code Playgroud)
然后,在控制器中,我检查声明:
[Authorize]
public IActionResult Index()
{
if (User.HasClaim("ihi:user_type", "clinician")) return Redirect("...");
if (User.HasClaim("ihi:user_type", "patient")) return Redirect("...");
if (User.HasClaim("ihi:user_type", "internal")) return Redirect("...");
throw new InternalErrorException("Logged in user is not in a recognized user role");
}
Run Code Online (Sandbox Code Playgroud)
我对这个方案有几个问题:
[Authorize]属性和我的声明检查)依赖于查看 cookie 的值。存储在该 cookie 中的数据是否相当安全,不会被篡改?(我希望如此,否则攻击者可以修改 cookie 并玩弄身份系统。)