Copied to clipboard

Flag this post as spam?

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


  • stephencai 21 posts 131 karma points
    Mar 08, 2019 @ 03:06
    stephencai
    0

    Currently, I am looking for a way to perform a single log on feature with other system through API.

    The feature logic is when user using other system account to login umbraco cms, backoffice will do the authenticate with calling API. If the username and password are correct and login to cms. There will also check if the user is not exist in cms, it will create new user and login.

    I am facing a problem about after create new user, suppose it can be auto login to cms. However, It shows "Login failed for user XXXX" at first time. It can be able to login when I login again.

    I am trying many way, eg. OwinStartup / custom member ship provider ... etc. But it does not fix my problem.

    Any advise?

  • Alexander Croner 71 posts 282 karma points
    Mar 11, 2019 @ 13:42
    Alexander Croner
    0

    We have it solved with an OwinStartup, we are using it in a few Umbraco projects and it works great.

    But your question is a little to wide to answer I think. What are showing in the logs etc? Can you show some code?

  • stephencai 21 posts 131 karma points
    Mar 15, 2019 @ 08:24
    stephencai
    0

    Sorry for late reply.

    Here is my code.

    [assembly: OwinStartup("CustomUmbracoOwinStartup", typeof(CustomUmbracoOwinStartup))]

    namespace bordrin.web { public class CustomUmbracoOwinStartup { public void Configuration(IAppBuilder app) { var applicationContext = ApplicationContext.Current; app.ConfigureUserManagerForUmbracoBackOffice

            app
                .UseUmbracoBackOfficeCookieAuthentication(ApplicationContext.Current)
                .UseUmbracoBackOfficeExternalCookieAuthentication(ApplicationContext.Current);
        }
    }
    

    }

    public class CustomBackOfficeUserManager : BackOfficeUserManager { public CustomBackOfficeUserManager(IUserStore

        if (authenticate)
        {
            var userService = ApplicationContext.Current.Services.UserService;
            var existingUser = userService.GetByEmail(user.UserName);
            if (existingUser == null)
            {
                CreateUser(user.UserName, password, user.UserName, userService);
            }
            Task.FromResult(BackOfficeUserPasswordCheckerResult.ValidCredentials);
    
        }
    
        return Task.FromResult(BackOfficeUserPasswordCheckerResult.InvalidCredentials);
    }
    
    private BackOfficeUserManager<BackOfficeIdentityUser> _userManager;
    protected BackOfficeUserManager<BackOfficeIdentityUser> UserManager
    {
        get { return _userManager ?? (_userManager = HttpContext.Current.GetOwinContext().GetBackOfficeUserManager()); }
    }
    
    private bool CreateUser(string username, string password, string email, IUserService userService)
    {
        var newUser = userService.CreateWithIdentity(username, email, password, userService.GetDefaultMemberType());
    
        newUser.RawPasswordValue = (Membership.Providers["UsersMembershipProvider"] as UsersMembershipProvider).HashPasswordForStorage(password);
    
        userService.Save(newUser);
    
        return true;
    }
    

    }

    Here is the capture at login page.

    enter image description here

    1 Login > api authentication

    2 success > check user > exist > login to cms

    2 success > check user > not exist > create user > login to cms

    I dont know why first time show error, and backend have created the user. when i click login again, it works fine.

Please Sign in or register to post replies

Write your reply to:

Draft