I'm trying to unlock a user account from the admin panel:
public async Task<ActionResult> EnableAccount(string id)
{
var user = UserManager.Users.FirstOrDefault(x => x.Id == id);
if(user != null)
{
await UserManager.ResetAccessFailedCountAsync(user.Id);
await UserManager.SetLockoutEndDateAsync(user.Id, null );
}
return RedirectToAction("Users");
}
I'm getting this error:
cannot convert from '<null>' to 'DateTimeOffset' in
await UserManager.SetLockoutEndDateAsync(user.Id, null);
Ended up doing it this way:
var user = UserManager.Users.FirstOrDefault(x => x.Id == id);
user.LockoutEndDateUtc = null;
user.LockoutEnabled = false;
Related
I wrote a piece of code in c# and I have a warning that I can't avoid and don't understand. first my code :
private async Task<ActionResult<List<string>>> UpdateImagesInBlob(List<BlobImage>? origin, List<BlobImage>? goal)
{
if (origin is null && goal is null)
return new BadRequestObjectResult("Update BlobImage Error");
if (goal is null)
{
var deleteOnlyResult = await RemoveImagesInBlob(origin!);
return deleteOnlyResult;
}
else if (origin is null)
{
var addOnlyResult = await AddImagesInBlob(goal);
return addOnlyResult;
}
var toDelete = origin.Where(im => !goal.Contains(im)).ToList();
var toAdd = goal.Where(im => !origin.Contains(im)).ToList();
IActionResult addResult, deleteResult;
addResult = await AddImagesInBlob(toAdd);
deleteResult = await RemoveImagesInBlob(toDelete);
if (addResult is OkObjectResult && deleteResult is OkObjectResult)
return new OkObjectResult(addResult);
else
return new BadRequestObjectResult("Update BlobImage Error");
}
(the code is not finished)
I use it like this :
var imageUpdateResult = await UpdateImagesInBlob(current.Images, new.Images);
if (imageUpdateResult is OkObjectResult)
I have a compilation warning Compiler Warning (level 1) CS0184
where is my issue ? my code return okObjectResult on success (addimageinblob and removeimageinblob return okObjectResult)
Is their something I missed ?
I have the following code that needs to return a single value. However, I keep on getting null (the field Notifications_Id = 0) even though it clearly exists in the database.
var role = _context.AssignedTickets.FirstOrDefault(a => a.Notifications_Id == incidence.Notifications_Id);
I am using Net Core 5
Below is my code
public async Task<ActionResult> EditTicket(int? id)
{
{
if (id == null)
{
return StatusCode(400);
}
Incidence incidence = await _context.Incidences.FindAsync(id);
if (incidence == null)
{
return StatusCode(401);
}
return View(incidence);
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditTicket(Incidence incidence)
{
var role = _context.AssignedTickets.FirstOrDefault(a => a.Notifications_Id == incidence.Notifications_Id);
if (role == null)
{
return StatusCode(404);
}
role.Status = "C";
role.ClosedOn = DateTime.Now;
if (ModelState.IsValid)
{
DateTime Timeup = (DateTime)role.ClosedOn;
DateTime Timedown = (DateTime)role.CreatedOn;
long DiffTicks = (Timedown - Timeup).Ticks;
role.TurnAroundTime = Math.Abs(DiffTicks).ToString();
_context.Entry(incidence).State = EntityState.Modified;
_context.SaveChangesAsync();
return RedirectToAction("Dashboard", "Agent");
}
return View(incidence);
}
Please use var role = _context.AssignedTickets.FirstOrDefault(a => a.Notifications_Id == 29); to get the object, and then compare whether the attributes inside match the database.
I read your post and all comments carefully. What everyone said is very reasonable, and the code in the post is not problematic.
So I think the problem is likely to be a problem with the string connecting to the database, that is, a different database is used.
I Have string model in my viewmodel i want to convert GetRolesAsync to string.
var string = await _userManager.GetRolesAsync(item);
>
'string' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'string'
I found the answer
var rolename = await _userManager.GetRolesAsync(model);
user1.RoleName = rolename[0].ToString();
Its works fine for me.
var user = await _userManager.Users.Where(x => x.Id == request.UserId).FirstOrDefaultAsync();
if (user != null)
{
Task<IList<string>> roles = _userManager.GetRolesAsync(user);
var returnUser = _mapper.Map<AspNetUser, AspNetUserDto>(user);
if (returnUser != null)
{
if (roles != null && roles.Result != null)
returnUser.Roles = roles.Result.ToList();
return returnUser;
}
}
I have the following code to reset the password of a user (who actually does not have a password):
ApplicationUser u1 = _userManager.FindByEmailAsync("aclowneyb8#un.org").Result;
string resetToken1 = _userManager.GeneratePasswordResetTokenAsync(u1).Result;
IdentityResult r1 = _userManager.ResetPasswordAsync(u1, resetToken1, "12345Da*").Result;
r1 returns succeed but the password is indeed not changed. I cannot login with the new password. Any suggestions? What I am missing?
You should use async and await in this case to execute your code
Below is my example
[HttpPost("ResetPassword"), ValidModel, AllowAnonymous]
public async Task<IActionResult> ResetPassword([FromBody]ResetPasswordViewModel model)
{
if (string.IsNullOrEmpty(model.Token) || string.IsNullOrEmpty(model.Email))
{
return RedirectToAction("Index", "Error", new { statusCode = AppStatusCode.NotFound });
}
var isResetTokenValid = await _userManager.CheckValidResetPasswordToken(model.Token, model.Email);
if (!isResetTokenValid || string.IsNullOrEmpty(model.Email))
{
return StatusCode(AppStatusCode.ResetPassTokenExpire);
}
var user = await _userManager.FindByEmailAsync(model.Email);
if (user == null)
{
return Ok();
}
await _userManager.ResetPasswordAsync(user, model.Token, model.Password); // use await here
return Ok();
}
The database is no working but no error come out, can someone explain to me what I did wrong. I want to update the column after the validation done.
public ActionResult Autherize(MoIS.Models.medicalassistant maModel)
{
using (DBModels dBModel = new DBModels())
{
var maDetails = dBModel.medicalassistants.Where(x => x.Username == maModel.Username && x.Password == maModel.Password).FirstOrDefault();
if (maDetails == null)
{
maModel.LoginErrorMessage = "Wrong username or password.";
return View("Index", maModel);
}
else
{
try
{
medicalassistant m = dBModel.medicalassistants.First(i => i.MaID == maDetails.MaID);
m.Name = "ali";
dBModel.SaveChanges();
Console.WriteLine("Updated");
}
catch (Exception ex) {
Console.WriteLine(ex.InnerException);
}
Session["maID"] = maDetails.MaID;
Session["username"] = maDetails.Username;
return RedirectToAction("Index", "Home");
}
}
}