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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
http://www.code-sample.com
http://www.code-sample.xyz

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.