Tuesday, October 21, 2014

How to Sign in as a Different User in SharePoint 2013 by Denis Stetsenko

Referred Link - http://blog.credera.com/technology-insights/microsoft-solutions/how-to-sign-in-as-a-different-user-in-sharepoint-2013/

SharePoint used to have a menu option called “Sign in as Different User” in the top-right corner of every page. It was a handy tool for developers and IT professionals in SharePoint 2007 and 2010, which allowed them to login with another account to test their solutions or customizations. Microsoft removed the option in SharePoint 2013, but you can still do it with workarounds or a one-click bookmarklet.
SharePoint-2010-2013-Sign-in-as-Different-User

Why Was This Feature Removed

I don’t know why Microsoft removed this feature. What I do know is it was never perfect and has a number of issues in SharePoint 2007 and 2010. Some of the most common issues are:
  • • Caching problems
  • • Documents opened in external applications are saved with an unexpected user account
  • • Right after user logs in with a different account, the page content shows information from the previous account and the cookies are not properly cleared
In other words, this feature was never meant as a security mechanism. Therefore, even though it’s possible to bring the menu option back, you should carefully consider whether doing so is really necessary. After all, it was removed for a reason.
If you want to bring the menu option back for all users, you can modify the Welcome.ascx file in the 15-hive on each server by following Nick Grattan’s blog post. If you just want to be able to log in with a different account without this menu option, read on.

How to Sign in as a Different User

Developers and/or IT administrators, myself included, often have to log in with a different user account to test their solutions or customizations, or get a higher-level permissions in a SharePoint site. Even though the menu option is missing, there are several workarounds that can be used to log in as a different user in SharePoint 2013.
Method 1: Internet Explorer’s Different User Option
The most common way to sign in as a different user is with Internet Explorer’s “Run as different user” option. Just right-click the IE icon while holding the shift key down and click on “Run as different user.” Once logged in, just navigate to the SharePoint site. This method is quite cumbersome but it does allow you to run multiple instances of IE and be logged in as a different user in each one.
Note for the Windows 7 or 8 users: if you have IE pinned to your taskbar, holding the shift key and right-clicking on the taskbar icon won’t work. You have to right-click the IE icon in the taskbar first, then hold the shift key and right-click on the Internet Explorer option.
Method 2: Navigate to the Close Connection Page
A slightly faster way to accomplish this is to navigate to the Close Connection page. Just visit the following URL in your browser:
http:///_layouts/closeConnection.aspx?loginasanotheruser=true
As soon as you hit this page, the familiar pop-up will prompt you for the username to log in with. Once you log in, you will be redirected to the home page.

Bookmarklet to Sign in as a Different User

Bookmarklet (bookmark + applet) is a web browser bookmark that contains JavaScript commands in the URL. This means we can build a bookmarklet that uses JavaScript Object Model in SharePoint 2013 to automate the second method above.
First, let’s build the script which uses SharePoint client context to redirect the user to the URL specified above:
  1. if (typeof SP !== 'undefined') {
  2.     var ctx = new SP.ClientContext.get_current();
  3.     var site = ctx.get_site();
  4.     ctx.load(site, 'Url');
  5.     ctx.executeQueryAsync(Function.createDelegate(thisfunction(sender, args) {
  6.         var url = site.get_url();
  7.         window.location = url + '/_layouts/closeConnection.aspx?loginasanotheruser=true';
  8.     }), Function.createDelegate(thisfunction (sender, args) {
  9.         alert('Error: ' + args.get_message());
  10.     }));
  11. else {
  12.     alert('Error: This is not a SharePoint 2010 or 2013 website');
  13. }
Basically, what we are doing is getting the client context of the current SharePoint site, loading the “Url” property of the “site” object via an AJAX request, and then redirecting the browser to the Close Connection page. If we fail to load the “site” object, we display the error. We also display an error if “SP” is undefined, which means that you are not on a SharePoint 2010 or 2013 website.
This script can be tested in the Console window of your browser’s development tools. Now we need to add this script to a hyperlink that can serve as a bookmarklet. To do so, just create an HTML file and add the following text to it, which includes the script above without line breaks:
  1. <a href="javascript: (function(){if (typeof SP !== 'undefined') { var ctx = new SP.ClientContext.get_current(); var site = ctx.get_site(); ctx.load(site, 'Url'); ctx.executeQueryAsync(Function.createDelegate(this, function (sender, args) { var url = site.get_url(); window.location = url + '/_layouts/closeConnection.aspx?loginasanotheruser=true'; }), Function.createDelegate(this, function (sender, args) { alert('Error: ' + args.get_message()); })); } else { alert('Error: This is not a SharePoint 2010 or 2013 website'); }})();">SP Login</a>
Open this HTML file in your favorite browser, and drag and drop this link to your bookmarks/favorites bar. Alternatively, you can just create a new bookmark in your browser, call it “SP Login” (or whatever else you like) and use the content of the “href” property of the hyperlink above in place of its URL. Unfortunately, some technical limitations are preventing me from adding this bookmarklet directly to this post.
Incidentally, this bookmarklet can also be used to log in as a different user in SharePoint 2010, since it also exposes the JavaScript Object Model. This can be useful in some scenarios, for example if you have a staging environment for your public site where the ribbon controls are only shown to the “admin” accounts, which are different from your main AD account you use in your organization.

No comments: