How to get last modifying user information using google drive API? - c#

In Google.Apis.Drive.v2.Data.File class it just provides name of the last modifying user. How to obtain full info of the user (like email, user id etc.)?
var service = new DriveService(auth);
Google.Apis.Drive.v2.Data.File file = service.Files.Get("fileid").Fetch();
file.LastModifyingUserName;// = "User Name" //How to get email id of this user?
In an organization there can be more than one person with the same first and last name. It is user id which differentiates. So I need email ID.
E.g Allan Donald => allan1#corp.com
Allan Donald => allan2#corp.com
This is very much possible.

I figured out how to do it in the Java API. It's far from elegant, but it works.
File file; // start with your file
User user = file.getLastModifyingUser();
Permission permission = service.permissions().get(file.getId(), user.getPermissionId()).execute();
String email = permission.getEmailAddress();
You can also use the RevisionList interface to get all the modifying users.

Related

Get Windows LiveID by first and last name

in my company, we're using MS Outlook and Skype. In both applications, I can click on several user to get information about them like the first name, the last name, availability, email adress or the information of WindowsIdentification.Name (i think, this is called the Live ID).
It looks like in the picutrue. I just erased personal information from the picture.
Is there a possibility to request this information of any user by their first and last name?
What I'm searching for is the function:
/*This function exists*/
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
But I don't want to know my own LiveID but the ID from some other user. So, I need something like:
/*This function does not exists*/
string UID = System.Security.Principal.WindowsIdentity.GetByName("Max Mustermann").Name;
Console.WriteLine(UID) /*prints DomainName/UserID*/
Is something like that available in .NET?
You can use the DirectorySeacher class to do this.
For example:
DirectorySearcher searcher1 = new DirectorySearcher(entry);
searcher1.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0})(sn={1}))", aName, aSName);
SearchResultCollection results1;
results1 = searcher1.FindAll();

google activity api limit result userid not working

i try to limit the list activity to a specific user but i always get the same results. i've tried users email address and the user id from the user list api function, both are not working.
i found a an open issue here, without an answer: https://code.google.com/p/google-apps-script-issues/issues/detail?can=2&start=0&num=100&q=&colspec=Stars%20Opened%20ID%20Type%20Status%20Summary%20Component%20Owner&groupby=&sort=&id=4974
my code:
ActivitiesResource.ListRequest listRequest = service.Activities.List();
listRequest.Source = "drive.google.com";
listRequest.DriveAncestorId = "root";
listRequest.UserId = "users email or Id";
listRequest.PageSize = 10;
IList<Activity> activities = listRequest.Execute().Activities;
additional info - i'm using a service account to authenticate
thanks
It is currently not possible to filter by user ID. Don't want to get into too many details, but the userId field has a different purpose that was never fully realized. Right now it's effectively ignored.

Authenticate AD user with alternate UPN suffix

This question might match with question at link here indirectly.
I am working on website project based on Asp.Net 4.0 for corporate use.
There is a form in website which ask users for their AD username and password with domain name selected by default.
I know of ways to authenticate user by root domain name. But there are users whose domain names(UPN suffix) had been modified.
For e.g. the domain name is xyz.com. So user is authenticate by user#xyz.com and their passwords. But for some users their name is user#abc.com.
So how to validate such users with alternative UPN suffix other than root domain name?
After lot of search with hit and trial method, I was able to formulate solution for it with reason.
The following link User Principal Name in AD by Jorge de Almeida Pinto is worth mentioning here. Please get details for iUPN and eUPN from there.
I am explaining my problem statement again as scenario to make it more clear.
Scenario
There are only two users in AD in domain (domain.com) named as Anil and Alex.
iUPN for Anil is Anil#domain.com and that of Alex is Alex#domain.com (which is by default set by AD itself).
eUPN for Anil is been left blank (which means it will be Anil#domain.com, the default behavior of AD). But for Alex it is been set as Alex#dummy.com for any reason.
You can get idea for AD interaction from link Active Directory With C# which I found nicely written.
As a programmer, I want to write code for making these both users get logged in AD from code.
Issues
Anil#domain.com get logged in successfully.
Alex#dummy.com cannot get logged in.
Reason
I had not been able to found perfect root cause for it.
But my guess is that, AD itself put domain name after # (at the rate). Since domain name for Alex is dummy.com, so AD tries to found user with suffix as #dummy.com. And return result as no user found.
Solution
The solution was to dissect username and domain name.
Append root domain name (domain.com) as suffix to user (with separate domain name). And then try to login.
You can have questions that other unauthorized user can also get in by this way. No! Because passwords need to be matched.
Why it worked?
Because AD was able to found user with Alex#domain.com in domain.com.
Edit
The solution I provided work only for case when other user is having same sAMAccountName with same domain name.
But what if the sAMAccountName is itself set with as Alex#dummy.com. So true solution was to go as -
(1) Get sAMAccountName on basis of UPN.
/// <summary>
/// Get sAMAccountName for matching UserPrincipalName (UPN)
/// </summary>
/// <param name="domain">Domain name</param>
/// <param name="userName">Username</param>
/// <returns></returns>
protected string GetSamUsername(string domain, string userName)
{
string samName;
using (var pc = new PrincipalContext(ContextType.Domain, domain))
{
var user = UserPrincipal.FindByIdentity(pc, userName); // Search for this user
if (user == null) return null; // If user is not there, why go forward
samName = user.SamAccountName;
}
return samName;
}
(2) Now logging in by any user will work.
It also helps us to authenticate user existence in AD.
If your first attempt fails (using default domain name), display the form with the domain name option.
Or, provide a textbox for the domain name that is filled out ahead of time that your users can modify if necessary.
When authentication fails, be sure to show them a message indicating they need to pay attention to the domain name you have shown.
UPDATE:
private void AuthenticateUser(string loginID, string pwd) {
var search = new DirectorySearcher(m_rootDir);
if (-1 < loginID.IndexOf("#")) {
search.Filter = "(&(objectClass=user)(SAMAccountName=" + loginID + "))";
} else { // this is their Common Name
search.Filter = "(&(objectClass=user)(cn=" + loginID + "))"; // Get User By Full Name
}
// more code here
}

How to tell if SPUser is Active Directory account

I'm working on a project where the client wants to restrict some content to only Active Directory users . Is there any way to identify that a SPUser is an AD user short of parsing the username string for the domain (or something along those lines). Something like SPUser.IsADUser would be awesome.
Edit
This seems to work, but I'm not sure if this is reliable enough? For this use case, identifying that a user is a windows user is enough (there are no local system accounts)
SPUser user = SPContext.Current.Web.CurrentUser;
string userName = user.LoginName.Substring(user.LoginName.IndexOf('|') + 1);
SPPrincipalInfo info = SPUtility.ResolveWindowsPrincipal(SPContext.Current.Site.WebApplication, userName, SPPrincipalType.User, false);
if(info != null){
//THIS IS A WINDOWS ACCOUNT
}
In my experience it is much better to use audiences for this purpose. You then can easily trim any web part using "Audience" property. You can read about audiences here. Of course it will only work if you have user profile synchronization configured.

DirectoryEntry results in The server is unwilling to process the request

I am writing a small app that integrates with AD and will be listing users and allowing members of the site to edit certain fields of the users. These fields will be, first name, last name, display name and email.
When I run the following code I get an exception that says
The server is unwilling to process the request.
Please note that result is not null and contains the correct active directory user that I want to edit. Also note that result is of type SearchResult. Also note that the user I am using to connect to AD is the Administrative user.
DirectoryEntry entryToUpdate = result.GetDirectoryEntry();
entryToUpdate.Properties["cn"].Value = user.Name;
entryToUpdate.Properties["mail"].Value = user.Email;
entryToUpdate.Properties["sn"].Value = user.Surname;
entryToUpdate.Properties["displayName"].Value = user.DisplayName;
string username = user.Email.Substring(0, user.Email.IndexOf("#"));
entryToUpdate.Properties["sAMAccountName"].Value = username;
entryToUpdate.CommitChanges();
Any Ideas?
I believe
entryToUpdate.Properties["cn"].Value = user.Name;
is your Problem. Use givenName as first Name. "cn" is managed by the System.
Also:
If using Exchange, the Mail attribute is managed by it.
Consider the ramifications of modifing samAccountName. Users may not know how to log in, seperate Systems with pseudo SSO may not recognize them, the Login Name does not match the local Profile Name, etc.

Categories