Notatki dla .NET w języku programowania C#

onClick checkbox add event

<input type="checkbox" onChange="$(this).val(this.checked? '1': '0');" />
</code

url.ToString() and url.OriginalString

var uri3 = new Uri("https://onet.pl/?param=value%20space%20in");
 
Assert.Equal("https://onet.pl/?param=value space in", uri3.ToString());
// true            
Assert.Equal("https://onet.pl/?param=value%20space%20in", uri3.OriginalString);
// true
Kategoria: 

Example

     var sssss = await _managerPlatformDataService.GetPlatformData();
 
            var acvite = sssss.Single(x => x.IsActive && x.Code == AMZEU.ToString()).Marketplaces;
 
            foreach (var Channel in partnerPlatformsResponse?.PartnerChannels)
            {
                var marketplaceCodes = Channel.PartnerMarketplaces.Select(m => m.CountryCode);
 
                var missingMarketplaceResponseDtos = acvite
                    .ExceptBy(marketplaceCodes, m => m.CountryCode)
                    .Select(m => new PDto(new PM
                    {
                        Marketplace = m,
                        PartnerChannelId = Channel.Id
                    })
                    { IsAvailable = false });
 
                Channel.PartnerMarketplaces = Channel.PartnerMarketplaces
                    .Concat(missingMarketplaceResponseDtos)
                    .OrderBy(x => _orderOfMarkets.IndexOf(x.CountryCode))
                    .ToList();
            }
Kategoria: 

AddClaimsPrincipalFactory

            services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
            {
                var allowed = options.User.AllowedUserNameCharacters + "ęóąśłżźćń";
                options.User.AllowedUserNameCharacters = allowed;
            }).AddEntityFrameworkStores<DomainModelContext>()
               .AddRoles<ApplicationRole>()
               .AddClaimsPrincipalFactory<ApplicationClaimsIdentityFactory>()
 
               .AddDefaultTokenProviders();

 public class ApplicationClaimsIdentityFactory : UserClaimsPrincipalFactory<ApplicationUser, ApplicationRole>
    {
        private readonly IHttpContextAccessor _httpContext;
 
        public ApplicationClaimsIdentityFactory(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager, IOptions<IdentityOptions> options, IHttpContextAccessor httpContext) : base(userManager, roleManager, options)
        {
            _httpContext = httpContext;
        }
 
        public override async Task<System.Security.Claims.ClaimsPrincipal> CreateAsync(ApplicationUser user)
        {
            ClaimsPrincipal principal = await base.CreateAsync(user);
 
            ClaimsIdentity claimsIdentity = (ClaimsIdentity)principal.Identity;
 
 
            var ddd = claimsIdentity.Claims.Where(x => x.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/name");
 
            foreach (var dd in ddd) {
                claimsIdentity.RemoveClaim(dd);
            }
 
            //claimsIdentity.Claims.ToList().RemoveAt(30);
 
            //claimsIdentity.Claims.Where(x => x.Type == )
            //claimsIdentity.AddClaim(new Claim("Deleted", user.Deleted.ToString().ToLower()));
 
 
            //_httpContext.HttpContext.User = principal;
 
 
            return principal;
        }
    }
Kategoria: 

entity tracking

  public async Task Update(Command command)
        {
            var p = await _repository.GetAsync(command.Id);
 
            foreach (var prop in command.GetType().GetProperties())
            {
                var value = prop.GetValue(command, null);
                var propertyInfo = p.GetType().GetProperty(prop.Name);
                if (propertyInfo != null)
                {
                    propertyInfo.SetValue(partner, value, null);
                }
            }
 
            _DbContext.ChangeTracker.DetectChanges();
            Console.WriteLine(_DbContext.ChangeTracker.DebugView.LongView);
 
            _DbContext.SaveChanges();
 
        }
<code>
Kategoria: 

Two-Factor Asp.core

var authenticatorCode = command.Code.Replace(" ", string.Empty).Replace("-", string.Empty);
var twoFactor = await _userManager.VerifyTwoFactorTokenAsync(user, _userManager.Options.Tokens.AuthenticatorTokenProvider, authenticatorCode);

https://chsakell.com/2019/08/18/asp-net-core-identity-series-two-factor-...
https://stackoverflow.com/questions/41084411/whats-the-difference-betwee...
https://blog.elmah.io/how-to-implement-two-factor-authentication-with-as...

Kategoria: 

Asp.core no caching controller methods

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
Kategoria: 

File asp.core Api swagger client NSwag

https://stackoverflow.com/questions/43844261/what-is-the-correct-way-to-download-a-file-via-the-nswag-code-generator-angular

1. Microsoft.AspNetCore.StaticFiles // To determine MimeType
2. NSwag.Annotations 

services.AddSwaggerGen(options =>
{
    // Swagger Configurations
    options.MapType<FileContentResult>(() => new OpenApiSchema
    {
        Type = "file"
    });
});

[HttpGet("Get_File")]
[SwaggerOperation("Get_File", OperationId = "Get_File")]
[SwaggerResponse(200, type: typeof(FileContentResult))]
[ProducesResponseType(typeof(FileContentResult), 200)]
public IActionResult GetFile()
{
    var filePath = "D:\\11.pdf"; // get file full path based on file name
    if (!System.IO.File.Exists(filePath))
    {
        return BadRequest();
     }
 
     return File(System.IO.File.ReadAllBytesAsync(filePath).Result, "application/octet-stream", "11.pdf");
}
Kategoria: 

entity rollbacl

Remove-Migration -context ***omainMo***xt  -Force
Kategoria: 

Edytor VS kropki

Program Visual Studio jest skonfigurowany do wyświetlania białych znaków.

Naciśnij Ctrl+ R, Ctrl+ W.

Jeśli używasz mapowania klawiatury w języku C #: (dzięki Simeon)

Naciśnij przycisk Ctrl+ E, S.

Jeśli chcesz skorzystać z menu: (dzięki angularsen)

Edit > Advanced > View White Space

Kategoria: 

Strony

Subskrybuj Notatki dla .NET w języku programowania C#