Tags » ASP.NET

HTML to text

Install-Package NUglify
public static IHtmlContent HtmlToText<T>(this IHtmlHelper<T> html, string htmlText)
    => new StringHtmlContent(Uglify.HtmlToText($"<div>{htmlText}</div>").ToString());
Dotnet

Add view location

In ASP.NET Core MVC, here’s how to add a new default location to find views:

services.AddControllersWithViews().AddRazorOptions(options => 
    options.ViewLocationFormats.Add("/Views/SpecialPlace/{0}.cshtml"))
Dotnet

TypeScript settings in csproj

These are the settings I put in the csproj file for TypeScript – it sets values to convert to ES5, takes all TypeScript files and compiles to individual JavaScript files in the… 77 more words

Aspnet

Add response compression in .NET Core

services.AddResponseCompression(options => options.Providers.Add<GzipCompressionProvider>());

app.UseResponseCompression();
Dotnet

ASP.NET end response without ThreadAbortException

Thanks to this answer on StackOverflow for this answer:

// Sends all currently buffered output to the client.
HttpContext.Current.Response.Flush(); 

// Gets or sets a value indicating whether to send HTTP content to the client. 33 more words
Dotnet

Claims Authorization Example

Startup.cs

using System.IO;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;

namespace WebApplication25
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            var cookieAuthSection = Configuration.GetSection("CookieAuth");
            string appName = cookieAuthSection.GetValue<string>("ApplicationName");
            string keyLocation = cookieAuthSection.GetValue<string>("KeyLocation");
            services.AddDataProtection()
                .SetApplicationName(appName)
                //.SetDefaultKeyLifetime(TimeSpan.FromDays(9999))
                .PersistKeysToFileSystem(new DirectoryInfo(keyLocation));

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, config =>
                {
                    config.Cookie.Name = "myapp";
                    config.LoginPath = "/Login";
                    config.AccessDeniedPath = "/AccessDenied";
                });

            services.AddAuthorization(config =>
            {
                config.AddPolicy("UserName", policyBuilder =>
                {
                    policyBuilder.RequireClaim(ClaimTypes.Name);
                });
                config.AddPolicy("Admin", policyBuilder =>
                {
                    policyBuilder.RequireClaim("IsAdmin");
                });
                config.AddPolicy("PhoenixAdmin", policyBuilder =>
                {
                    policyBuilder.RequireClaim("CityAdmin", "Phoenix");
                });
                config.AddPolicy("Zero", policyBuilder =>
                {
                    policyBuilder.RequireAssertion(context =>
                    {
                        var roleClaim = context.User.Claims.FirstOrDefault(x => x.Type == "Roles");
                        if (roleClaim == null) { return false; }

                        var roles = JsonConvert.DeserializeAnonymousType(roleClaim.Value, new[] { new { RoleCode = "", OrgID = 0 } });
                        return roles.Any(r => r.OrgID == 0);
                    });
                });
                config.AddPolicy("AdminOrCityAdmin", policyBuilder =>
                {
                    policyBuilder.RequireAssertion(context =>
                    {
                        return context.User.Claims.Where(x => x.Type == "IsAdmin" || x.Type == "CityAdmin").Any();
                    });
                });
            });

            services.AddRazorPages();
        }

        // This method gets called by the runtime. 814 more words
Dotnet

OOP atau Object Oriented Programming merupakan salah satu pattern pada programming yang sangat umum digunakan oleh developer di dunia. Kalau berbicara mengenai OOP pasti kaitannya sangat erat dengan bahasa pemograman Java.  82 more words

Javascript