How to assign site for new user via Kentico API - c#

I'm trying to create a new Kentico user and assigning them to a site, via Kentico API: https://docs.kentico.com/api11/configuration/users#Users-Assigningausertoasite
In my case SiteContext.CurrentSiteName returns empty string. I thought of trying by hard coding the site.
UserInfor newUser = new UserInfo();
//...set all required properties...
//add user to site
UserInfoProvider.AddUserToSite(newUser.UserName, "My Site Code(or Display) Name");
//save user
UserInfoProvider.SetUserInfo(newUser);
Tried both Display Name as well as Code Name of the site in UserInfoProvider.AddUserToSite, but the user is not getting assigned to the site on Kentico site. It does get created with all the properties.

You have to use SiteInfoProvider to get the site code name
var si = SiteInfoProvider.GetSites()
.WhereEquals("SiteId",123)
.FirstOrDefault();
UserInfoProvider.AddUserToSite(newUser.UserName, si.SiteName);

Just had to add user to the site after saving the user.
//save user
UserInfoProvider.SetUserInfo(newUser);
//add user to site
UserInfoProvider.AddUserToSite(newUser.UserName, "My Site Code Name (not display name)");

Related

In AWS Cognito set temporary password for existing user and set status to "Enabled / FORCE_CHANGE_PASSWORD"

Is there an admin api to set a temporary password for an existing user and set the account back to "Enabled / FORCE_CHANGE_PASSWORD"?
We are in the early stages of changing authentication in an old winform app to use AWS Cognito. We are not allowed to count on users having email or sms (plant floor). We have created new users in the pool and supplied a first time temporary password. The users are in "Enabled / FORCE_CHANGE_PASSWORD" status. We tested this and the first time they log in with temp password we get the Cognito challenge and they then get the enter new password screen.
I cannot find any page or doc besides AdminCreateUser that sets password and status of account. All seem to rely on flow that involves verified email or phone.
My "google-foo" may be off so asking the question.
Here is the code in a console app we created to add the user...
var request = new AdminCreateUserRequest()
{
Username = user.COGNITO_ID,
UserPoolId = COGNITO_POOL_ID_USEAST,
TemporaryPassword = user.Password
};
var cognitoClient = new AmazonCognitoIdentityProviderClient(creds, Amazon.RegionEndpoint.USEast1);
var result = cognitoClient.AdminCreateUserAsync(request).Result;
return "User created as Enabled / FORCE_CHANGE_PASSWORD";
I could delete and re-add the user (they have no attributes) but want to avoid this.
You can use AdminUpdateUserAttributes to update the Account Status to FORCE_CHANGE_PASSWORD. If that doesn't work you can simple add a custom attribute which acts as a flag for accounts you want to disable. Then you can simply add a lambda post login which checks for this flag and forces user to change his password.

"Encoded" login names in SharePoint Client Object Model

I'm writing a small .NET proof-of-concept console app that performs a series of actions on a SharePoint document library. I noticed that the following methods expect an "encoded" login name - that is, login name including provider information, e.g. i:0#.w|DOMAIN\user.
context.Web.EnsureUser(encodedLoginName);
context.Web.SiteUsers.GetByLoginName(encodedLoginName);
How do I reliably convert a user name such as DOMAIN\user to this encoded format in the SharePoint Client Object Model?
I've read a couple of blog posts that address this issue with the SPClaimProviderManager, which is not available in the client API.
I am able to get the encoded login name using the ResolvePrincipal utility method:
using SP = Microsoft.SharePoint.Client;
//...
// resolve user principal using regular login name or e-mail:
var userPrincipal = SP.Utilities.Utility.ResolvePrincipal(
context,
context.Web,
"DOMAIN\\user", // normal login name
SP.Utilities.PrincipalType.User,
SP.Utilities.PrincipalSource.All,
context.Web.SiteUsers,
false);
context.ExecuteQuery();
// ensure that the user principal was resolved:
if (userPrincipal.Value == null)
throw new Exception("The specified user principal could not be resolved");
// get a User instance based on the encoded login name from userPrincipal
var user = context.Web.SiteUsers.GetByLoginName(userPrincipal.LoginName);
context.Load(user);
context.ExecuteQuery();
This seems to work as intended. However, if there is a better way or caveats that I should be aware of, please let me know.

How to get Current User in Sharepoint site workflow 2010?

Am trying to find the current logged user in sharepoint site workflow 2010 while creating a project. Based on the user, I would like to retrieve the current user's project manager. Every time am trying to retrieve current user name, it's giving System Account.
I even tried logging in as different user but still displaying System Account as the current user.
I tried following options :
SPUser user = workflowProperties.OriginatorUser;
SPUser user = SPContext.Current.Web.CurrentUser;
SPWeb web = SPContext.Current.Web;
SPUser user = web.CurrentUser;
SPContext.Current.Web.CurrentUser.LoginName;
But everything failed. Am sure that am doing something wrong. I don't know the correct procedure. Some procedures give me null or Object reference not set to an instance of the object or System Account details. I have even tried using elevated permission and its giving me null value.
SPSecurity.CodeToRunElevated elevatedSubmit = new SPSecurity.CodeToRunElevated(delegate
{
//SPUser user = SPContext.Current.Web.CurrentUser;
//string strAssignedTo = user.Name;
string sspURL = "http://localhost/PWA/default.aspx";
SPSite site = new SPSite(sspURL);
SPWeb web = site.OpenWeb();
SPUser theUser = web.CurrentUser;
string strUserName = theUser.Name;
});
SPSecurity.RunWithElevatedPrivileges(elevatedSubmit);
Am I supposed to add users explicitly as SPUser or any other changes before trying to retrieve current user via workflow ?
SharePoint 2010 Get Current Username / Logged in User
check this StackExchange answer as well
Get the current user interacting with a site workflow
if you are wanting to get the current user when you log in you can try something like this
SPWeb webSite = SPControl.GetContextWeb(SPContext);
SPUser spUser = webSite.CurrentUser;
string strUserName = spUser.LoginName;
using this line below will return the OriginatorUser however if you are not logged in as Admin you will get the System Account UserName
//This give the Login name e.g <domain>\<name>
workflowProperties.OriginatorUser.LoginName;
** Note ** I noticed that in your code you are trying to get / assign user twice
you should only need this line if you decide to use your code
SPUser user = SPContext.Current.Web.CurrentUser;
its seem to work :
SPUser user = this.workflowProperties.OriginatorUser;
RunWithElevatedPrivileges gives you system account privileges in addition to the privileges you would get with a reverto call.
is that code executed on SPSecurity.RunWithElevatedPrivileges method ??
Here is a another trick that i found :
string ModifiedbyUserName = Convert.ToString(workflowProperties.Item.GetFormattedValue("Modified By"));
see this : logged-in user in workflow
Helps it helps!!

How to get the facebook user token with a facebook application

I have a facebook application with has to post updates on specific facebook page. The application has rights to post updates on the facebook wall of the page's admin. But how can i get the (admin's) users access token, which i need to get the access token of the page. Do i have to save the access tokenthe first time the user allows the app to post (offline access)?
Thanks for your reply!
I use the Facebook C# SDK as well. You'll need the following permissions:
manage_pages
read_stream
publish_stream
HTTP Get to "me/accounts" and get the .data object off of the returned value. This will be a list of the following:
var list = ApiGet("me/accounts").data;
foreach (dynamic acc in list)
{
var name = (string)acc.name;
var accessToken = (string)acc.access_token;
var category = (string)acc.category;
var pageId = (string)acc.id;
}
As you can see, each one of the pages have their own unique access token.

Show photos from my facebook page on my web site

I have some doubts:
I want to show the photos from my company facebook page on my company web site using C# SDK.
How can I do that with an access token which never expires? Or why my app id and app secret is not enough?
var fb = new FacebookWebClient("app_id", "app_secret");
If I only use this it says:
An active access token must be used to query information about the current user.
Basically I see non-sense to ask for a valid access token because it's not about to interact with any of my visitors' page but just simply show my photos from my company facebook page to them.
UPDATE: To get my app token I am doing:
var oauthClient = new FacebookOAuthClient
{
AppId = "app_id",
AppSecret = "app_secret"
};
dynamic result = oauthClient.GetApplicationAccessToken();
accessToken = result.access_token;
But now, how I can show photos from my account?
If I do it like:
var fb = new FacebookWebClient(accessToken);
//Get the album data
dynamic albums = fb.Get("app_id/albums");
I receive an empty JSON but I am sure that my account on facebook has several albums.
Tokens can and do expire. You must have code in place to handle when it does.
Your appid and app secret are not enough because at least one of the page admins MUST grant access to the page to get photo data via the Graph API.
Remember, the API is not only Facebook's sandbox, but they also are the ones who built it, so you're going to have to play by their rules.

Categories

Resources