我的应用程序的注册工作流程允许仅在确认电子邮件地址后设置用户名。使用 Microsoft.AspNetCore.Identity 1.1.1.0 中的默认值和实体框架会在数据库中生成一个 AspNetUsers 表,其中包含可为空的 UserName 列。但是,如果我尝试向 UserManager.CreateAsync() 提供具有 null UserName 的 IdentityUser 实例,则会收到错误:“用户名 '' 无效,只能包含字母或数字。”。
我想避免设置临时的非空值,因为用户名应该是唯一的,但不是电子邮件地址。其他解决方法(例如 guid)看起来就像是一个 hack。
允许空用户名为空的最佳方法是什么?
我已经使用个人用户帐户创建了默认的 ASP Net Core MVC Web 应用程序。我向 AspNetUsers 表添加了一个自定义 UserID INT 字段,并将其作为外键链接到所有其他自定义表,以便我可以跟踪用户在数据库中插入/更新的记录。我这样做是因为我更喜欢使用 INT 字段作为主键。在这种情况下,一切正常。
我现在正在考虑使用外部身份提供程序,例如 Azure Active Directory,但是我无法弄清楚如何将 Azure AD(或任何其他身份提供程序)中的用户链接到我的本地数据库表以维护用户 ID 外键约束。从我所有的研究中,我找不到任何关于这种情况的文章。
如何将外部身份提供商的用户信息链接到我的本地数据库表?我担心我可能需要在外部身份提供程序和本地 AspNet 用户表中双重处理用户帐户管理,以使用其他自定义表维护外键,这看起来很疯狂。
也许我遗漏了一些明显的东西,或者我的方法都是错误的,所以如果有更好的替代方案来跟踪谁在使用外部身份提供程序的 ASP Net Core Web 应用程序的数据库表中做了什么,我会很乐意采用它。
authentication azure-active-directory asp.net-core-mvc asp.net-core-identity
使用 UserManager 或 DbContext 是否有缺点或优点?
如果我使用这个:
public class UserManager<TUser> : IDisposable where TUser : class
public virtual Task<TUser> FindByIdAsync(string userId);
Run Code Online (Sandbox Code Playgroud)
或者,如果我使用直接 dbcontext,例如:
var user = dbContext.Users.Where(x => x.Id == model.Id).FirstOrDefault();
// Login like this
await HttpContext.SignInAsync(..)
Run Code Online (Sandbox Code Playgroud) 我正在使用 ASP.NET Core Identity 在我的网站上进行身份验证,并且我试图在用户登录后立即将用户 ID 记录到数据库中,但我似乎无法使用以下代码访问它们。此处返回GetResult()null,但如果它在下一页上,则它可以正常工作,但只是不在OnPostAsync它需要的方法中。
Input.UserIDOrLogonName不包含用户 ID。
private readonly SignInManager<User> _signInManager;
private readonly UserManager<User> _userManager;
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(Input.UserIDOrLogonName, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
var userId = _userManager.GetUserAsync(HttpContext.User)
.GetAwaiter().GetResult().UserId;
_audit.RecordAction(AuditType.LoginSuccess, userId);
return LocalRedirect(returnUrl);
}
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以访问 User 实例吗?
仅当我在共享托管上部署 ASP .NET Core 2.1 Web 应用程序时,才会出现此问题。我将 Azure Key Vault 与 PersistKeysToFileSystem 结合使用。
Web 应用程序在我的开发计算机上以及使用 PersistKeysToFileSystem 的带或不带 Azure Key Vault 的 Azure 应用程序上运行良好。
fail: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[48]
An error occurred while reading the key ring.
System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of …
c# key-management azure asp.net-core-identity asp.net-core-2.1
我正在使用 dotnet-core3-preview 中的新 ASP.NET Core Identity API Authorization,文档位于https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity- api-authorization?view=aspnetcore-3.0
我成功地运行了典型的登录过程,并且在不记名令牌中设置并发送了令牌。但是现在我有一个 api 端点,它应该从数据库中返回一些用户详细信息,所以我试图从令牌中提取用户 ID 来查询数据库。
但是,根据我下面的屏幕截图,我无法在任何声明中找到 id,我该如何实现?
[HttpGet]
[Authorize]
public async Task<IActionResult> GetUserByToken(){
var ls = User.Claims.AsQueryable();
return Ok(ls);
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有 ASP.NET Core 后端的 iOS 应用程序,它使用 IdentityUser 令牌,它可以使用用户名/密码登录来检索令牌,如下所示:
var result = await _signInManager.PasswordSignInAsync(model.Username, model.Password, isPersistent: true, lockoutOnFailure: true);
if (result.Succeeded)
{
return Ok(result);
}
Run Code Online (Sandbox Code Playgroud)
但现在我想添加“使用 Apple 登录”的选项,这在应用程序端很容易完成,在 Apple 验证用户后,我们会得到一个已签名并包含电子邮件地址的 IdentityToken (JWT)。这看起来很简单,我可以将该 IdentityToken 传递回我的 API 并通过使用以下方法检查签名来验证它是否有效:https : //stackoverflow.com/a/34423434/1999217
但据我了解(以及到期),Apple 提供的 IdentityToken 只能使用一次并转换为新令牌(在我的情况下为 Microsoft.AspNetCore.Identity 令牌)。
我怎样才能做到这一点?一旦我验证了 IdentityToken 并且我相信请求用户拥有传入的电子邮件地址,那么我可以检索 ApplicationUser 和 Microsoft.AspNetCore.Identity.UserManager 似乎有一个方法 SignInAsync() 但它的返回类型不是 IdentityResult 作为预期所以它没有帮助。
我还查看了 ExternalLoginProvider 方法,但它依赖于我认为不适合这种情况的回调,因为我有一个可以信任的令牌。
我正在考虑使用电子邮件地址并进行即时密码重置,但感觉很狡猾。就像是:
var user = await _userManager.FindByEmailAsync(email);
var token = await _userManager.GeneratePasswordResetTokenAsync(user);
var result = await _userManager.ResetPasswordAsync(user, token, RandomPassword());
Run Code Online (Sandbox Code Playgroud)
因为这与身份验证有关,所以我觉得安全总比抱歉好。我想我的问题的核心是:这很狡猾吗?有没有更好的方法来获得只有电子邮件的 IdentityResult?
在很难让我的区域显示端点路由后,我设法在这个自我回答的线程中修复了它(尽管不是以非常令人满意的方式):从 2.2 迁移到 3.0 后出现问题,默认工作但无法访问区域,无论如何要调试端点解析?
然而,Identity UI 根本没有显示,我在挑战时被重定向到正确的 url,但页面是空白的。我添加了身份 UI 块包,并且从 mvc 路由更改为端点路由,我没有更改任何应该破坏它的内容。
即使我像在 hack 中那样添加路由,我似乎也没有做与默认项目所做的事情和身份在那里工作的事情有多大不同。
通常问题隐藏在线路周围而不是在线上,我发布了我的整个启动文件。
常规(默认)控制器工作。管理区域工作(其中一个页面没有身份验证,我可以访问它)任何其他管理区域页面将我重定向到 /Identity/Account/Login?ReturnUrl=%2Fback(预期行为)但该页面以及任何其他页面我测试的 /Identity 页面是空白的,在调试中运行时没有错误并且连接了调试器。
非常感谢任何帮助,完整启动如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using FranceMontgolfieres.Models;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
namespace FranceMontgolfieres
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This …Run Code Online (Sandbox Code Playgroud) c# asp.net-core asp.net-core-identity razor-pages asp.net-core-3.0
使用 ASP.NET Core 我正在创建一个系统来邀请用户加入一个组,获得免费积分,...
当邀请用户加入组时,我创建了一个保存在数据库中的邀请:
令牌与其他信息一起保存在数据库中:
Invitation invitation = new Invitation {
InvitationType = "JoinGroup",
Completed = false,
Expiry = DateTime.Now.AddDays(4),
Token = some_token,
Parameters = new List<Parameter> {
new Parameter { Name = "GroupId", Value = 22 },
new Parameter { Name = "RoleId", Value = "Admin" },
new Parameter { Name = "Email", Value = "someuser@name.com" },
}
}
Run Code Online (Sandbox Code Playgroud)
然后我发送一封带有网址的电子邮件:
/invite?token=some_token
Run Code Online (Sandbox Code Playgroud)
当用户访问 url 时,我会使用给定的令牌获取记录。
有了这些信息,我可以做任何我需要做的事情,例如,将用户添加到组中。
题
我应该如何创建一个独特的令牌?
我应该在令牌中包含哪些信息?
我应该如何验证它?
c# .net-core asp.net-core asp.net-core-identity asp.net-core-2.2
我有一个无法使用 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)