Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • SSA Backend 108 posts 640 karma points
    Aug 16, 2016 @ 09:47
    SSA Backend
    0

    Get Umbraco "IMemberService" from web api "AuthorizationFilter"

    I have a UmbracoMemberBasicAuthenticationFilter class which is actually a web api filter to ensure Basic authentication for my APIs. Actually I need an instance of an object which implements IMemberService interface which I can get from ApplicationContext. The question is can I get ApplicationContext inside filter or get an instance for my service via some (unknown for me) umbraco container.

    public class UmbracoMemberBasicAuthenticationFilter : BasicAuthenticationFilter {
    protected override bool OnAuthorizeUser(string username, string password, HttpActionContext actionContext)
    {
        IMemberService memberService = actionContext.GetMagicUmbracoApplicationContext.Services.MemberService(); // 1
        IMemberService memberService = someUmbracoContainer.Resolve<IMemberService>(); // 2
        return true;
    } }
    
  • Sven Geusens 169 posts 878 karma points c-trib
    Aug 16, 2016 @ 13:14
    Sven Geusens
    0

    I don't know of a way to get the Services inside a class that doesn't inherit from certain umbraco classes (like UmbracoApiController).

    If you only need basic functionality you can always do the following:

    public class testfilter
    {
        protected static UmbracoHelper Umbraco
        {
            get
            {
                return new UmbracoHelper(UmbracoContext.Current);
            }
        }
    
        protected override bool OnAuthorizeUser(string username, string password, HttpActionContext actionContext)
        {
            return Umbraco.MembershipHelper.Login(username, password);
        }
    }
    

    I don't know if this suffices for you but it's worth a try.

  • Damiaan 440 posts 1295 karma points MVP 5x c-trib
    Aug 16, 2016 @ 13:23
    Damiaan
    1

    Just a small remark on the "umbraco container". There is no dependency injection which is done in Umbraco (yet, in v7 that is). The member service is not registered as an MVC service (as far as i know). So you can not get it from the default resolver.

    Also, I would not use any MemberService on front-end code if not needed as it would hit the database.

    Is there any reason why you can not use [Authorize()] ?

    Kind regards Damiaan

  • SSA Backend 108 posts 640 karma points
    Aug 17, 2016 @ 07:43
    SSA Backend
    0

    Hi Damiaan,

    Thank you for your response. Now I have a clear vision about dependency injections. I need to achieve register/login users for mobile application. We decided to use Basic Auth and Umbraco Members because they don't have access to backoffice. The question is can I use Authorize attribute or inheriting from UmbracoAuthorizedApiController for members? Will it use Basic Auth or work with cookies?

    UPD. From the source code UmbracoAuthorizeAttribute - ensures authorization is successful for a back office user

    Best regards

  • Damiaan 440 posts 1295 karma points MVP 5x c-trib
    Aug 18, 2016 @ 14:52
    Damiaan
    0

    You have found the answer already, but just for completeness, the documentation does mention the authorizedControllers. One would deduct the attribute is also for the backoffice. So your code inspection completely right. Well done! :-)

  • SSA Backend 108 posts 640 karma points
    Aug 17, 2016 @ 13:10
    SSA Backend
    101

    Finally I have found solution. As discussed here we need to create custom behavior to support Basic Auth because Umbraco use cookies. This steps will help you to achieve the goal:

    1. Create authentication filter. You can ready code here
    2. Create umbraco filter based a previous filter

      public class UmbracoMemberBasicAuthenticationFilter : BasicAuthenticationFilter{ public UmbracoMemberBasicAuthenticationFilter()
      {
      
      
      }
      public UmbracoMemberBasicAuthenticationFilter(bool active) : base(active)
      {
      
      
      }
      protected override bool OnAuthorizeUser(string username, string password, HttpActionContext actionContext)
      {
          var helper = new MembershipHelper(UmbracoContext.Current);
          return helper.Login(username, password);
      }}
      
    3. Use it in your action

        [HttpGet]
        [UmbracoMemberBasicAuthenticationFilter]
        public HttpResponseMessage Login()
        {
            return Request.CreateResponse(HttpStatusCode.OK);
        }
    
  • Damiaan 440 posts 1295 karma points MVP 5x c-trib
    Aug 18, 2016 @ 14:54
    Damiaan
    0

    Happy to see you have found the solution.

    Using the membershipHelper is a better choice than the Services. Thumbs up for this one.

    Thanks for sharing!

Please Sign in or register to post replies

Write your reply to:

Draft