Custom Authentication and Authorization in MVC 5

custom Authentication and Authorization in MVC5

/// <summary>
/// Override onAuthorization filter is use for authorization use request.
/// </summary>
protected override void OnAuthorization(AuthorizationContext filterContext)
{
      AuthorizationManager authorizationManager = new AuthorizationManager();
      string FilePath = Convert.ToString(filterContext.HttpContext.Request.FilePath);
      if (!authorizationManager.IsAuthorized(_userSession, FilePath))
      {
         RedirectToControllers(ControllerHelper.ACCOUNT,                                                            ControllerHelper.Action.ACCOUNT_LOGIN);
      }
}


/// <summary>
/// This method is used for check the current request is authorized or not.
/// </summary>
#region Authorization public methods
public bool IsAuthorized(UserSession sessions, string url)
{
    bool IsIsAuthorize = false;
    if (sessions != null)
    {
        if (sessions.UserType != null && sessions.UserType != "")
        {
            IsIsAuthorize = new Authorizations().IsAuthorized(sessions, url);
        }
    }
    return IsIsAuthorize;
 }
 #endregion


/// <summary>
/// This method is used to get the authorized URL list with help of current user-type.
/// and call the the authorization repository Authorized method.
/// </summary>
public bool IsAuthorized(UserSession userSession, string url)
{
     return new AuthorizedRepository().Authorized(userSession.UserType, url);
}



/// <summary>
/// This method is used to return the authorized URLs.
/// </summary>
public bool Authorized(string UserType, string url)
{
     bool authorized = false;
     AuthorizedURL Urls = base.Context.AuthorizedURLs.Where(x => x.UserType == UserType && x.URL == url && x.IsActive == true).SingleOrDefault();
     if (Urls != null)
     {
          authorized = true;
     }
     return authorized;
}


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.