Custom Authentication and ASP.NET MVC

前端 未结 5 1384
独厮守ぢ
独厮守ぢ 2020-12-13 00:50

I have an internal web app being built in ASP.NET 4. We are stuck with using an authentication API built by another team. If a user to the site is authenticated

5条回答
  •  星月不相逢
    2020-12-13 01:36

    You probably want to have a custom authorization filter. Here's an example: Custom filters in MVC. You can then apply this filter globally on app start (using RegisterGlobalFilters).

    public class LegacyAuthorize : AuthorizeAttribute
    {
      public override void OnAuthorization(HttpActionContext actionContext)
      {
        if (HttpContext.Current.Session["User"] == null)
          base.HandleUnauthorizedRequest(actionContext);
      }
    }
    

    Then in your global.asax you'd have something like this:

    GlobalFilters.Filters.Add(new LegacyAuthorize());
    

提交回复
热议问题