How can I login programmatically into Sitecore?

久未见 提交于 2019-11-28 04:43:18

问题


How can I login programmatically into Sitecore? For example if you would like to connect a small part of the Sitecore API to a desktop application, you would need to login into sitecore first to access the databases etc.

Can this be done?


回答1:


As Mark said, you will need to create a web service that your desktop app will talk to. If you need to deal with permissions in that service you have two options.

  1. Use a SecurityDisabler to make your webservice run in the context of an Admin user.

    using (new Sitecore.SecurityModel.SecurityDisabler())
    {
        // do stuff here
    }
    
  2. For more specific control you can use a UserSwitcher.

    From the Security API Cookbook page 34

    string domainUser = @"domain\user"; 
    
    if (Sitecore.Security.Accounts.User.Exists(domainUser)) 
    { 
        Sitecore.Security.Accounts.User user = 
        Sitecore.Security.Accounts.User.FromName(domainUser,false); 
    
        using (new Sitecore.Security.Accounts.UserSwitcher(user)) 
        { 
            //TODO: code to invoke as user 
        } 
    } 
    



回答2:


Not really. What you can do, however, is write a supporting web service for your desktop application, and have that run in a Sitecore context.




回答3:


There is an easier way to accomplish your goal:

  1. Create a desktop application and reference the same version of the Sitecore binaries that your web app uses.
  2. Configure your desktop application to point to the same Sitecore DBs as your web site.
  3. Use the security disabler and then set the context as follows: Sitecore.Context.SetActiveSite("website");

Sitecore may tell you that what you are trying to do will not work. But trust me, this works and I've used this method in a project before.

Additional details: Sitecore uses the Master, Core and Web DBs as it's data store. My suggested method uses Sitecore APIs to write directly to the DBs. When using this method, you'll need to be aware of the cache implications.

cheers!



来源:https://stackoverflow.com/questions/3234832/how-can-i-login-programmatically-into-sitecore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!