Copied to clipboard

Flag this post as spam?

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


  • Vijitha 8 posts 98 karma points
    May 27, 2022 @ 03:59
    Vijitha
    0

    Azure AD Auto login for Members

    I am able to add member login for the website with active directory. Right now, I have a button which user has to click and then the azure ad login is taking place. How can I change this to auto login?

  • James Brooksbank 8 posts 79 karma points
    Oct 03, 2022 @ 07:26
    James Brooksbank
    0

    So I don't know if you are still having the same issue, but i found a "hack" which allows us to move forward. We have used Azure AD on projects not in Umbraco and I couldn't figure out how to implement this in Umbraco.

    So i went with another approach. On the main template page i put a fragment in which loads up the Umbraco form for logging in with OpenID, then i put in a javascript function which automatically submits the form starting the Azure login process.

    I have included the code i used below. I know this probably isn't best use and I'm open for other techniques but this has done the job for us.


    Below i check that the user is logged in and has a matching member in the back office, if they are logged in and have a member then i set the flag for isLoggedIn to true.

        bool isLoggedIn = false;
    MemberIdentityUser member = null;
    
    if (Context.User?.Identity?.IsAuthenticated == true)
    {
    
        if (_memberManager.IsLoggedIn()) {
            member = await _memberManager.FindByNameAsync(User.Identity.Name);
    
            if (member != null)
            {
                isLoggedIn = true;
            }
    
        }
    
    }
    

    Then i use the following code which loads the form if the flag isLoggedIn is false

        @if (!isLoggedIn) {
        <div id="loginContainer">
            @using (Html.BeginUmbracoForm<UmbExternalLoginController>(nameof(UmbExternalLoginController.ExternalLogin)))
            {
                <input type="hidden" name="provider" id="provider" value="UmbracoMembers.OpenIdConnect" />
            }
        </div>
    }
    

    The i use the following javascript to submit the above form if it exists.

        <script type="text/javascript">
        $( document ).ready(function() {
            var container = $('#loginContainer');
    
            if (container.length) {
                var form = container.find('form')[0];
                form.submit();
            }
    
        });
    </script>
    

    I'm fully aware that this is not the best way and i'm sure there must be another way, but due to time frames and client requirements this has done the job for us. I hope it helps

Please Sign in or register to post replies

Write your reply to:

Draft