text
stringlengths 0
13.4k
|
|---|
return BadRequest("Could not add item.");
|
}
|
return RedirectToAction("Index");
|
}
|
[ValidateAntiForgeryToken]
|
public async Task<IActionResult> MarkDone(Guid id)
|
{
|
if (id == Guid.Empty)
|
{
|
return RedirectToAction("Index");
|
}
|
var currentUser = await _userManager.GetUserAsync(User);
|
if (currentUser == null) return Challenge();
|
var successful = await _todoItemService
|
.MarkDoneAsync(id, currentUser);
|
if (!successful)
|
{
|
return BadRequest("Could not mark item as done.");
|
}
|
return RedirectToAction("Index");
|
83
|
Using identity in the application
|
}
|
Both service methods must now accept an ApplicationUser parameter.
|
Update the interface definition in ITodoItemService :
|
Task<bool> AddItemAsync(TodoItem newItem, ApplicationUser user);
|
Task<bool> MarkDoneAsync(Guid id, ApplicationUser user);
|
And finally, update the service method implementations in the
|
TodoItemService . In AddItemAsync method, set the UserId property
|
when you construct a new TodoItem :
|
public async Task<bool> AddItemAsync(
|
TodoItem newItem, ApplicationUser user)
|
{
|
newItem.Id = Guid.NewGuid();
|
newItem.IsDone = false;
|
newItem.DueAt = DateTimeOffset.Now.AddDays(3);
|
newItem.UserId = user.Id;
|
// ...
|
}
|
The Where clause in the MarkDoneAsync method also needs to check for
|
the user's ID, so a rogue user can't complete someone else's items by
|
guessing their IDs:
|
public async Task<bool> MarkDoneAsync(
|
Guid id, ApplicationUser user)
|
{
|
var item = await _context.Items
|
.Where(x => x.Id == id && x.UserId == user.Id)
|
.SingleOrDefaultAsync();
|
// ...
|
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.