Copied to clipboard

Flag this post as spam?

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


  • Stefan Fernström 6 posts 26 karma points
    May 13, 2022 @ 08:06
    Stefan Fernström
    0

    Check if a Member is a User

    Hello everyone!

    I am a developer coding a solution where i want to check if a Member is also a User/backoffice user and add support for our logging. the code looks like this.

    public bool AddAndOrLoginMember()
        {
            bool succeeded = false;
            var user = GetUser();
            if (!CheckValidAbonnemang(user))
            {
                return false;
            }
    
            var memberService = Current.Services.MemberService;
    
            _logger.Info<LoginService>("Check if user already exists");
            IMember member = memberService.GetByUsername(user.Personnummer);
    
            var backOfficeUserService = Current.UmbracoContext.Security.CurrentUser;
            if (member != null)
            {
                _logger.Info<LoginService>($"User, {user.Fornamn} {user.Efternamn}: Username {user.Personnummer.Substring(0, 10)}, exists. Login user.");
    
                // Member already added. Login member
                succeeded = Login(member);
            }
            else
            {
                // Create the user
                member = AddUmbracoMember(user);
    
                // Login user
                _logger.Info<LoginService>("Login user");
                succeeded = Login(member);
            }
    
            if (succeeded)
            {
                _logger.Info<LoginService>($"Set forms authentication cookie");
                FormsAuthentication.SetAuthCookie(member.Username, true);
            }
    
            _logger.Info<LoginService>($"User login succeeded: {succeeded}");
    
            return succeeded;
        }
    

    I found Current.UmbracoContext.Security.CurrentUser; and it has a method i think called IsAdmin() but it only takes a IUser and when i get the current member its a IMember so it aint compatible.

    My first thought was to do something like If()member != null || backOfficeUserService.IsAdmin(member){...} but then i get a "Severity Code Description Project File Line Suppression State Error CS1501 No overload for method 'IsAdmin' takes 1 argument"

    so any idea how to easily solve this and do a check if a member is also a backofficeUser? if more clarification is needed i will try to answer it :)

  • Corné Hoskam 80 posts 587 karma points MVP 3x c-trib
    May 13, 2022 @ 13:49
    Corné Hoskam
    0

    Hi Stefan,

    There is a clear difference between Members and Users. Users are specifically accounts that have access to the Umbraco Back-Office. Members are used for external users of the platform, like the front-end of your application!

    You can for example check if your Users and Members have the same username or e-mail address if you are still interested in such a thing, but there is no intended relationship between Members and Users as it stands!

    Hope this helped!

    Kind regards,

    Corné Hoskam

Please Sign in or register to post replies

Write your reply to:

Draft