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 Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

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