Showing posts with label login. Show all posts
Showing posts with label login. Show all posts

Thursday, 3 May 2012

Get Current User Email, Login, Display Name Details

working on a requirement on getting the current user's Email id in efficient way.
The normal code which microsoft given fails:
SPWeb site = SPContext.Current.Web;
SPUser user = site.CurrentUser;
string DisplayName = user.Name;
string Login = user.LoginName;
string EMail = user.Email;
string User Notes = user.Notes;

Because most of the users who don't have access to All sites won't give details of their email by the code given. If you are wondering what is all about the difference between All users see below:
SPWeb site = SPContext.Current.Web;
SPUserCollection c1 = site.Users;
SPUserCollection c2 = site.AllUsers;
SPUserCollection c3 = site.SiteUsers;


The code gives 3 types of different user collection so i guess the user who logged in and didn't find his email by the code above belongs to one of this group.
The difference between these SPUserCollection is copied from MSDN.:
The Users collection has the smallest membership of these three collections. This collection includes all the external principals that have been explicitly assigned permissions within the current site.

The AllUsers collection includes all members of the Users collection, plus external users that have accessed objects within the site using implicit permissions through group or role membership. For example, imagine a user named Brian with the login of LITWAREINC\BrianC that has never been given explicit permissions to access a site and view a particular list. However, he might still be able to view the list because of his membership within an Active Directory group that has been configured with list view permissions. When Brian first accesses the site or one of its objects (say, a list using implicit permissions), he is added as a member of the AllUsers collection, but he is not added as a member of the Users collection.

The SiteUsers collection is an aggregation that combines membership for each AllUsers collection within the current site collection. The membership of this collection includes all external principals that have been assigned permissions to any object within the site collection as well as all external users that have been granted access to any of the site collection's objects using implicit permissions.

I used a basic way to get the current user using (context of control HTTPCONTEXT) Context.User.Identity.Name or Page.User.Identity.Name which does the same httpcontext.

After we get the current user login i can pass it to the magic of another Class in sharepoint object Model which brings the user details.

using Microsoft.SharePoint.Utilities;

SPWeb osite = SPContext.Current.Web;

SPPrincipalInfo prin = SPUtility.ResolvePrincipal(osite,Context.User.Identity.Name , SPPrincipalType.All, SPPrincipalSource.All, osite.AllUsers, false);
writer.Write(prin.Email);

using this method u can also search the user by their Email ID, or their Display name. its a cool method who does the search on multiple fields.

Monday, 19 December 2011

Why I Can not login via wp-admin but same time I can login from WP-login.php

I know how it feel, like no one know the solution. You like to pay everything you have, to find out why you can not login into your wordpress from wp-admin. And if you do it will redirect to same page without showing any message. but same time you can login into your wordpress from wp-login.php.  Sometime clearing the browser’s cookies might let us login again but to be honest it’s a pain, and clearing your cookies isn’t a permanent fix and do not work everyone.

Answer is easy when you know the following two solutions.

Solution1:  At   WordPress admin–> Settings–> General Settings:

“WordPress address (URL)”  should be same as “Blog address (URL)”

Solution2:  Fixing  issue with cookies.

problem is that occasionally on our various WordPress sites the wp-admin.php URL login stops working and we can only login through wp-login.php.

What we need to do is to force WordPress to follow the correct cookie path. This can be done by adding a small line of code to the wp-config.php file located in your WordPress installation root directory.

Open up wp-config.php in your web editor and add this:

/** wp-admin login fix */
@define('ADMIN_COOKIE_PATH', '/');


If you’re unsure, it can go just before the ?> at the end of the php code like so:

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
/** wp-admin login fix */
@define('ADMIN_COOKIE_PATH', '/');
?>


Update:  Some reported WP Security Scan case this issue. Please test and confirm in comments if you are using WP Security Scan wordpress plugin.

Alternative Cron


Use this, for example, if scheduled posts are not getting published.
"this alternate method uses a redirection approach,
which makes the users browser get a redirect when the cron needs to run,
so that they come back to the site immediately while cron
continues to run in the connection they just dropped.
This method is a bit iffy sometimes, which is why it's not the default."

define('ALTERNATE_WP_CRON', true);

define('DISABLE_WP_CRON', true);