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
| Articles = this.storage.Articles.Include(a => a.Photos).OrderBy(a => a.Id).Take(20).ToList().Select( | |
| a => new ArticleViewModelFactory(this.storage, this.cache).Create(a) | |
| ) |
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
| Categories = this.storage.Categories.AsNoTracking().ToList().Select( | |
| c => new CategoryViewModelFactory().Create(c) | |
| ) |
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 IndexViewModel Create() | |
| { | |
| return new IndexViewModel() | |
| { | |
| Categories = this.storage.Categories.ToList().Select( | |
| c => new CategoryViewModelFactory().Create(c) | |
| ), | |
| Articles = this.storage.Articles.OrderBy(a => a.Id).Take(20).ToList().Select( | |
| a => new ArticleViewModelFactory(this.storage).Create(a) | |
| ) |
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 Category | |
| { | |
| public int Id { get; set; } | |
| public string Name { get; set; } | |
| public ICollection<Article> Articles { get; set; } | |
| } | |
| public class Article | |
| { |
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
| dbContext.CountByRawSql("SELECT COUNT(*) FROM YourTable WHERE X = @X", new KeyValuePair<string, object>("@X", 5)); |
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 static int CountByRawSql(this DbContext dbContext, string sql, params KeyValuePair<string, object>[] parameters) | |
| { | |
| int result = -1; | |
| SqlConnection connection = dbContext.Database.GetDbConnection() as SqlConnection; | |
| try | |
| { | |
| connection.Open(); | |
| using (SqlCommand command = connection.CreateCommand()) |
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
| @inject AspNetCoreCustomUserManager.IUserManager UserManager | |
| @using System.Security.Claims | |
| <h1>ASP.NET Core<br />Custom User Manager</h1> | |
| <div class="form__field field"> | |
| <span class="marker marker--secondary">User.Identity.IsAuthenticated:</span> @User.Identity.IsAuthenticated | |
| </div> | |
| @if (this.User.Identity.IsAuthenticated) | |
| { | |
| <div class="form__field field"> | |
| <span class="marker marker--secondary">UserManager.GetCurrentUser(this.Context).Name:</span> @UserManager.GetCurrentUser(this.Context).Name |
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 HomeController : Controller | |
| { | |
| private IUserManager userManager; | |
| public HomeController(IUserManager userManager) | |
| { | |
| this.userManager = userManager; | |
| } | |
| [HttpGet] |
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
| services.AddScoped<IUserManager, UserManager>(); |
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
| private IEnumerable<Claim> GetUserClaims(User user) | |
| { | |
| List<Claim> claims = new List<Claim>(); | |
| claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())); | |
| claims.Add(new Claim(ClaimTypes.Name, user.Name)); | |
| claims.AddRange(this.GetUserRoleClaims(user)); | |
| return claims; | |
| } |