Skip to content

Instantly share code, notes, and snippets.

@Ibro
Created April 24, 2018 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ibro/65105607bb5fef4479c0c464f97d0644 to your computer and use it in GitHub Desktop.
Save Ibro/65105607bb5fef4479c0c464f97d0644 to your computer and use it in GitHub Desktop.
Categories - create with AuthService in action
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,UserId")] Category category)
{
ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", category.UserId);
// If ModelState isn't valid, return to view immediately
if (!ModelState.IsValid)
{
return View(category);
}
var authorizationResult = await _authorizationService.AuthorizeAsync(User, category, new InsertCategoryRequirement());
if (!authorizationResult.Succeeded) // If user has no access show him 403
{
return new ForbidResult();
}
// Add category, save changes and redirect to Category/Index - Categories List page
_context.Add(category);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment