我将身份放入已创建的项目中,但它没有找到该Account/Login页面。
它没有本地化页面,这是controller:
[Route("[controller]/[action]")]
public class AccountController : Controller
{
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger _logger;
public readonly Erp.Models.ErpDb _context;
public AccountController(Erp.Models.ErpDb context, SignInManager<ApplicationUser> signInManager, ILogger<AccountController> logger)
{
_signInManager = signInManager;
_logger = logger;
_context = context;
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
_logger.LogInformation("User logged out.");
return RedirectToPage("/Index");
}
}
Run Code Online (Sandbox Code Playgroud)
Startup
public class Startup
{
private IHostingEnvironment env;
private IConfigurationRoot config;
public Startup(IHostingEnvironment env)
{
this.env = env;
var builder = …Run Code Online (Sandbox Code Playgroud) 在 ASP.NET Core 2.2 MVC 中,我试图获取特定角色的所有用户的列表。
外汇。名为“Admin”角色的所有用户的列表:
var idsWithPermission = _userManager.GetUsersInRoleAsync("Admin").Result;
var users = _db.ApplicationUser.Where(u => idsWithPermission.Contains(u.Id)).ToListAsync();
return(users);
Run Code Online (Sandbox Code Playgroud)
编译器在此处失败“u.Id”:idsWithPermission.Contains(u.Id)
错误:参数 1:无法从“字符串”转换为 Microsoft.AspNetCore.Identity.IdentityUser
这是一个新手问题,所以对于鲨鱼来说可能非常简单:-) 提前非常感谢......
我正在学习在 asp.net core 中使用 Identity 进行登录/注册,我正在尝试在注册时添加错误,这是我的代码
if (ModelState.IsValid)
{
var user = new IdentityUser
{
UserName = model.Username,
Email = model.Email,
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var signInresult = await _signInManager.PasswordSignInAsync(model.Username, model.Password, false, false);
if (signInresult.Succeeded)
{
return RedirectToAction("Index");
}
}
else
{
List<IdentityError> errorList = result.Errors.ToList();
string errors = "";
foreach (var error in errorList)
{
errors = errors + error.Description.ToString();
}
return Content(errors);
}
return Redirect("Index");
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是 errorList 中的错误是一一返回的,而不是全部返回,例如,我得到“用户名已被占用”或“密码必须包含字母”。但不是同时全部。我该如何解决这个问题?
我启动了一个新的 Blazor 应用程序,并尝试使用 Entity FrameworkCore。我想链接 Identity AspNetUsers 表。我想要与 UserAddress 表建立一对多关系。一个 AspNetUser 有多个地址。
public class UserAddress
{
public string Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
' I don't know what to put here to connect this table with the AspNetUsers Table
public int UserId {get;set} This doesn't work
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何让 EF 构建 AspNetUsers Id 和 UserAddress 表之间的一对多关系
asp.net entity-framework asp.net-identity entity-framework-core asp.net-core
我正在开发一个系统,该系统允许用户使用他们的电子邮件或电话号码作为用户名进行注册。我们想在允许登录之前确认这是有效的。我们将 ASP.NET Core Identity 与 Identity Server 4 结合使用。
我找到了需要确认电话和确认电子邮件的说明(https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-configuration?view=aspnetcore-3.1#登录),但我找不到如何将它们用作已确认电子邮件或电话的 OR 选项。
有一个RequireConfirmedAccount属性,但这似乎只检查电子邮件,如果仅确认电话号码则失败。
有没有办法让我覆盖这个,或者注入我自己的登录管理器来强制执行这个?
UserManager.GetUserAsync(authState.User)UserManager除非已经被调用,否则会抛出异常。
例如;
这引发NullReferenceException了await UserManager.GetUserAsync(authState.User);
@ page "/"
@inject UserManager<ApplicationUser> UserManager
<AuthorizeView>
...
</AuthorizeView>
@code{
[CascadingParameter]
private Task<AuthenticationState> authenticationStateTask { get; set; }
protected async override Task OnInitializedAsync()
{
var authState = await authenticationStateTask;
var currentUser = await UserManager.GetUserAsync(authState.User); // exception here
}
}
Run Code Online (Sandbox Code Playgroud)
但这工作正常;
@ page "/"
@inject UserManager<ApplicationUser> UserManager
<AuthorizeView>
...
</AuthorizeView>
@code{
[CascadingParameter]
private Task<AuthenticationState> authenticationStateTask { get; set; }
protected async override Task OnInitializedAsync()
{
var allUsers = UserManager.Users.ToList(); // hack …Run Code Online (Sandbox Code Playgroud) 我试图使用以下函数从数据库中获取“DocumentationPage”类型的对象:
CurrentDocumentationPage = await documentationPageService.GetDocumentationPageById(DocumentID);
Run Code Online (Sandbox Code Playgroud)
在这种情况下我正在使用
[Parameter] public Guid DocumentID { get; set; }
Run Code Online (Sandbox Code Playgroud)
NavigateTo这与函数一起传递
public void OnNavItemClick(Guid Id)
{
navMan.NavigateTo($"/View/" + Id);
}
Run Code Online (Sandbox Code Playgroud)
要从我的数据库中获取相应的 Guid。我的DocumentationPage课程如下所示:
public class DocumentationPage : Entity
{
public string UserId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime Published { get; set; }
public DateTime Updated { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
实体所在位置:
public class Entity
{
public Guid Id;
} …Run Code Online (Sandbox Code Playgroud) 我有一个使用 Identity 进行身份验证的 Blazor WASM 应用程序,该应用程序在周五运行良好。昨晚我去进行更改,在 Azure 中运行的应用程序和在我的计算机上本地运行的应用程序在启动后都开始出现异常。从一切正常运行到现在,环境或代码库没有任何变化。
The app runs, redirects to the login page, the get is processed fine on the server but throws an exception on the first line of markup in the .cshtml file (assuming because it is failing to encrypt the content at that point):
An unhandled exception occurred while processing the request. CredentialUnavailableException: EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, string additionalMessage) …
我有一个 ASP.NET Core MVC Web 应用程序,它使用身份来处理用户帐户身份验证和授权。在我的本地 IIS Express 上运行该应用程序时,一切正常。将应用程序部署到共享网络服务器后,我开始注意到登录的用户帐户会以看似随机的间隔被注销。通过实验,我能够确定无论帐户处于活动状态还是空闲状态,都会发生注销。它们的发生没有重复的时间间隔,并且与我在 cookie 上设置的任何到期时间完全无关。Web 应用程序中的每个视图都会发生注销,因此我无法将问题固定到任何特定的控制器。此外,我对应用程序的已发布版本和本地测试版本使用相同的数据库,因此使用相同的用户帐户。我任何人都知道从哪里开始寻找解决方案,我将不胜感激。
我需要构建一个 ASP.NET Core 托管的 Blazor Web assembly 应用程序 (.NET 6),其中所有应用程序功能都隐藏在登录墙后面,但我不确定如何最好地实现这一点。对于身份验证/授权,我使用 ASP.NET Identity 和 IdentityServer。
到目前为止,我已经创建了一个名为的新 Razor 组件Login.razor,它仅包含一个触发内置身份验证过程的链接:
<a href="authentication/login">Log in</a>
Run Code Online (Sandbox Code Playgroud)
该组件具有该@page指令"/",因此它是用户访问应用程序时登陆的第一个“页面”。
这工作得很好,但是一旦用户成功登录,他们就会被重定向到返回 URL,作为 .NET 身份过程的一部分,在本例中,这是一个现在无用的登录页面。
我不想只替换身份页面中的返回 URL 并将用户重定向到另一个特定页面,因为我认为返回 URL 在向用户发送指向特定页面的链接的情况下非常有用。例如,如果我尝试在不先登录的情况下导航到 mywebsite.com/fetchdata 等受保护资源,它会触发 Blazor 附带的任何身份验证魔法,让用户登录,然后在用户登录后将其重定向到 /fetchdata成功地做到了。我想保留这个功能。
"/index"如果用户来自 Login.razor 组件,我需要做什么才能让服务器重定向到另一个页面(例如)?或者我只是以完全错误的方式处理这一切?非常感谢任何建议。
asp.net-identity asp.net-core blazor blazor-webassembly hosted-blazor-webassembly