This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace SpanOfT_Dev | |
| { | |
| public class Program | |
| { | |
| private static void Main(string[] args) | |
| { | |
| int[] arr = { 0, 1, 2, 3, 4, 5 }; | |
| Span<int> span = arr.AsSpan(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int[] arr = { 0, 1, 2, 3, 4, 5 }; | |
| Span<int> span = arr.AsSpan(); | |
| foreach (var number in span) | |
| { | |
| Console.WriteLine(number); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [HttpPost] | |
| [ValidateAntiForgeryToken] | |
| [AuthorizeResource(typeof(InsertCategoryRequirement))] | |
| 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) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CategoriesController : Controller | |
| { | |
| private readonly ApplicationDbContext _context; | |
| private readonly IAuthorizationService _authorizationService; | |
| public CategoriesController(ApplicationDbContext context, IAuthorizationService authorizationService) | |
| { | |
| _context = context; | |
| _authorizationService = authorizationService; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [HttpPost] | |
| [ValidateAntiForgeryToken] | |
| public async Task<IActionResult> Create([Bind("Id,Name,UserId")] Category category) | |
| { | |
| var authorizationResult = await _authorizationService.AuthorizeAsync(User, category, new InsertCategoryRequirement()); | |
| if (!authorizationResult.Succeeded) // If user has no access show him 403 | |
| { | |
| return new ForbidResult(); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class InsertCategoryHandler : AuthorizationHandler<InsertCategoryRequirement, Category>, IAuthorizationRequirement | |
| { | |
| private readonly ISecurityService _securityService; | |
| public InsertCategoryHandler(ISecurityService securityService) => _securityService = securityService; | |
| protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, | |
| InsertCategoryRequirement requirement, Category category) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [HttpPost] | |
| [ValidateAntiForgeryToken] | |
| public async Task<IActionResult> Create([Bind("Id,Name,UserId")] Category category) | |
| { | |
| // Populate UserId select list. | |
| ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", category.UserId); | |
| // If ModelState isn't valid, return to view immediately | |
| if (!ModelState.IsValid) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string> | |
| { | |
| public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) | |
| : base(options) | |
| { | |
| } | |
| public DbSet<Category> Categories { get; set; } | |
| } |