English 中文(简体)
在 ASP. NET 核心身份中添加/删除用户角色时错误 。
原标题:"User security stamp cannot be null." error when adding/removing user roles in ASP.NET Core Identity
I am trying to add roles to a user if they do not match the ones that it already has and remove those that do match, but I am getting the following error: User security stamp cannot be null. Controller: [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(string id, User user, string[] roles) { if (id != user.Id) { return NotFound(); } if (ModelState.IsValid) { try { var listOfRoles = await privateUser.GetRolesAsync(user); foreach (var role in listOfRoles.Except(roles)) { await privateUser.RemoveFromRoleAsync(user, role); } foreach (var role in roles.Except(listOfRoles)) { await privateUser.AddToRoleAsync(user, role); } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(user.Id)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(user); }
最佳回答
You re using a user obtained via a POST, which is not only bad practice, but won t even work here, because you re not posting the full User object. Namely, the value of SecurityStamp is missing, which is what the exception is telling you. Do not post the User. Instead, use the User.Id to fetch it from the database: [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(string id, string[] roles) { var user = await _userManager.FindByIdAsync(id); if (user == null) { return NotFound(); } // the rest of your code } UPDATE (for modifying user at the same time) [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(string id, User model, string[] roles) { var user = await _userManager.FindByIdAsync(id); if (user == null) { return NotFound(); } // map over the values from "model" (i.e. the posted "User") // use "user", not "model" for role management functions user.FirstName = model.FirstName; // the rest of your code }
问题回答

暂无回答




相关问题
.NET 7 minimal API request timeout

I found this in .NET 8 (prerelease) but I can t use it due to company policy. https://learn.microsoft.com/en-us/aspnet/core/performance/timeouts?view=aspnetcore-8.0&viewFallbackFrom=aspnetcore-7.0 ...

Blazor web assembly pass checkbox list values to model

I m new to Blazor. I m working in a web assembly Blazor project. I am trying to create a form that passes values back to a model. I have it working with input text fields and drop downs, but I am ...

asp.net 6 c# mvc one to many get data from many side

I am trying to access the data on the many side of a one to many relationship. In the Index action of my controller I assign the data to a variable and return it. I have a Blog, the one side, with ...

How to update shared view adminheader.cshtml from a model

I ve added a drop down into my shared adminheader.cshtl view and it needs to be dynamically updated from data from a table. So when I use another view like routeplan.cshtml I load the view from ...

Gitlab CI On Prem, Docker Image and ASP.NET Core 7

We have a .NET 6 application. We added CI using: image: mcr.microsoft.com/dotnet/sdk:6.0 before_script: - dotnet restore --packages $NUGET_PACKAGES_DIRECTORY build: stage: build script: - ...

ASP.NET Core 2 .1 - How to show all data in table

I don t know how to put CustomerData into CustomerLists property. I m already using CustomerLists = CustomerData; but I got error: CustomerLists is a type but is used like a variable Can anyone ...

热门标签