Showing posts with label Asp.net Core. Show all posts
Showing posts with label Asp.net Core. Show all posts

12 September 2022

ASP.NET Core disable authentication in development environment

    You can bypass authorization in development environment by applying AllowAnonymousAttribute() to your endpoints. as below code 


 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

        {

           //-----------------------

            app.UseRouting();

            app.UseEndpoints(endpoints =>

            {

                if (env.IsDevelopment())

                    endpoints.MapControllers().WithMetadata(

                        new AllowAnonymousAttribute());

                else

                    endpoints.MapControllers();

            });

        //---------------------------

          }

13 July 2022

How to Get the Current User in ASP.NET Core

 Create new Controller and add below code:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Dotnet_Core_MVC.Controllers
{
    public class ExpHttpContextAccessorController : Controller
    {
        private readonly IHttpContextAccessor _httpContextAccessor = new HttpContextAccessor();
        public IActionResult Index()
        {
            string userName = _httpContextAccessor.HttpContext.User.Identity.Name;
            ViewBag.userName = userName;
            return View();
        }
    }
}

Go to View and add below code:

 <b>Welcome:@ViewBag.userName</b>

Update ConfigureServices() in Startup.cs as below:

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddHttpContextAccessor(); 
        }

How to use ViewBag inside of Filters of Asp.net Core

 Create ViewBag in Action Filter as below: 

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
namespace Dotnet_Core_MVC.CustomActionFilters
{
    public class CustomActionFilter : Attribute, IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Controller is Controller controller1)
            {
                controller1.ViewBag.MyName = "Adi";
            }
        }
        public void OnActionExecuting(ActionExecutingContext context)
        {
           
        }
    }
}

Use ViewBag in your View Pages as Below:

 <b>Welcome:@ViewBag.MyName</b>

11 January 2022

How to Add Custom header at IAction method in Asp.net Core

 [HttpGet]

[Route("GetStudents")]

public IActionResult GetAllStudents()

{

var obj = new Student

{

Name = "Adi",

Id = 1

};

HttpContext.Response.Headers.Add("x-custom-header-from-action 1", "my custom value 1");

return Ok(obj);

}

1 August 2021

How to enable CORS in ASP.NET Core

 open startup.cs page .net core project

In ConfigureServices() method:

public void ConfigureServices(IServiceCollection services)

{

 --------                 

  services.AddCors();

 --------

}

In Configure() method:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

  {

  --------

   app.UseCors(builder =>

          builder.AllowAnyOrigin()

          .AllowAnyHeader()

          .AllowAnyMethod()

        );

  --------

  }