mvc 5 web api base controller

MVC 5 Web API Base Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc.Filters;

namespace myPortal.API
{
    /// <summary>
    /// Base api controller will extend to all apis. And can handle authentication and authorization here.
    /// </summary>
    public class BaseAPIController : ApiController
    {
        /// <summary>
        /// BaseAPIController constructor will check that identity exists or not.
        /// </summary>
        public BaseAPIController()
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    SetHeader();
                }
        }

        /// <summary>
        /// Set header status code to 401
        /// </summary>
        public void SetHeader()
        {
            HttpResponse resp = HttpContext.Current.Response;
            resp.StatusCode = 401;
            resp.End();
        }
    }

}
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.