How to add an User Content through Sensenet Client? - sensenet

I want to add an User Content of Sensenet from a client which developed by .NET.
Please give me a solution.
Thanks so much.

You should be able to create users the same way as any other content, by providing the parent, the content type name and a few mandatory fields.
The example below assumes that you already initialized the client, you have the /Root/IMS/MyDomain/MyOrgUnit in your repo and there is no existing user there with the same name.
var user = Content.CreateNew("/Root/IMS/MyDomain/MyOrgUnit", "User", "johnsmith");
user["LoginName"] = "johnsmith"; // best practice: same as the content name in the line above
user["Password"] = "123456789";
user["FullName"] = "John Smith";
user["Email"] = "johnsmith#example.com"; // this has to be unique under the domain
user["Enabled"] = true; // to let the user actually log in
await user.SaveAsync();
Let us know if you encounter any errors.

Related

How can I change the "key" name from an action field in Zapier (Scripting) with the KEY_pre_write method?

I was wondering how I can change the key of an action field through scripting in Zapier. I know I need to use KEY_pre_write to change an element before I send the request but how can I call the specific action field and change the key name in something else?
Zapier scripting image
The key name is currently "type" but I want to change it for instance to "type1".
Thanks in advance.
Fixed it myself btw, maybe for someone who needs to know.
'use strict';
var Zap = {
CompanyAdd_pre_write: function(bundle) {
var actionfields = bundle.action_fields;
var stringify = JSON.stringify(actionfields);
var body = stringify.replace("type1", "type"); // renaming key
bundle.request.data = body;
console.log(actionfields);
return bundle.request;
}
};
David here, from the Zapier Platform team.
key is set by the key of the action itself. See here:
Those would be action_pre_write, broken_js_pre_write, etc. Feel free to tweak these for a private app, but note that it'll break any existing zaps that use that action.
​Let me know if you've got any other questions!

Auth0 Get userId in response payload?

When a user logins using the Auth0 lock on my client side, I get an idToken, but also an idTokenPayload which looks like this:
idTokenPayload = {
audience: "AUTH0CLIENTID",
exp: 1494190538,
iat: 1494154538,
iss: "AUTH0DOMAIN"
sub: "USERNAME"
};
Would it be possible to return the userId in Auth0's database instead of the username in the sub field?
The reason I want to do this is that I want to keep Auth0's db for users, and I have on my server-side some Profile, Post, Comment etc entities which have a userId column. Right now before each request on my entities I need to populate the user by doing an extra request: let id = Profile.find("... where username === auth0.sub").getId(); (pseudo-code of course).
With the C# lock sdk, you get back an Auth0User after the call to the LoginAsync method in the Auth0 client. Let's call this variable auth0User. If I look at auth0User.Profile, a JObject (it's a JSON object if you're not using C#), it contains a JSON array named "identities". My identities variable initialization looks like:
var identities = (JArray)auth0User.Profile["identities"];
This array contains all the identity providers associated with the user. If like me you haven't attached any other sign in besides Auth0, there will be just 1 entry here. Each object in this JSON array will contain a "provider" string and a "user_id" string. If the provider says "auth0" then it's from Auth0. Since I don't use FB or other account types I'm not exactly sure what they say. Here's my C# code to get the UserID:
var identities = (JArray)auth0User.Profile["identities"];
if (identities != null)
{
foreach (var identity in identities)
{
var provider = (string)identity["provider"];
if (string.Equals(provider, "auth0"))
{
UserID = (string)identity["user_id"];
break;
}
}
}
I believe that this should all be provided standard without needing to add any rules or webhooks. This article should explain in more detail and also gives examples in javascript: auth0 normalized user profile

ASP.net - Uploading Files Associated with a Database Record?

I know that there are tons of examples of multi-part form data uploading in ASP.net. However, all of them just upload files to the server, and use System.IO to write it to server disk space. Also, the client side implementations seem to handle files only in uploading, so I can't really use existing upload plugins.
What if I have an existing record and I want to upload images and associate them with the record? Would I need to write database access code in the upload (Api) function, and if so, how do I pass that record's PK with the upload request? Do I instead upload the files in that one request, obtain the file names generated by the server, and then make separate API calls to associate the files with the record?
While at it, does anyone know how YouTube uploading works? From a user's perspective, it seems like we can upload a video, and while uploading, we can set title, description, tags, etc, and even save the record. Is a record for the video immediately created before the API request to upload, which is why we can save info even before upload completes?
Again, I'm not asking HOW to upload files. I'm asking how to associate uploaded files with an existing record and the API calls involved in it. Also, I am asking for what API calls to make WHEN in the user experience when they also input information about what they're uploading.
I'm assuming you're using an api call to get the initial data for displaying a list of files or an individual file. You would have to do this in order to pass the id back to the PUT method to update the file.
Here's a sample of the GET method:
[HttpGet]
public IEnumerable<FileMetaData> Get()
{
var allFiles = MyEntities.Files.Select(f => new FileMetaData()
{
Name = f.Name,
FileName = f.FileName,
Description = f.Description,
FileId = f.Id,
ContentType = f.ContentType,
Tags = f.Tags,
NumberOfKB = f.NumberOfKB
});
return allFiles;
}
Here's a sample of the POST method, which you can adapt to be a PUT (update) instead:
[HttpPost]
[ValidateMimeMultipartContentFilter]
public async Task<IHttpActionResult> PutFile()
{
try
{
var streamProvider =
await Request.Content.ReadAsMultipartAsync(new InMemoryMultipartFormDataStreamProvider());
//We only allow one file
var thisFile = files[0];
//For a PUT version, you would grab the file from the database based on the id included in the form data, instead of creating a new file
var file = new File()
{
FileName = thisFile.FileName,
ContentType = thisFile.ContentType,
NumberOfKB = thisFile.ContentLength
};
//This is the file metadata that your client would pass in as formData on the PUT / POST.
var formData = streamProvider.FormData;
if (formData != null && formData.Count > 0)
{
file.Id = formData["id"];
file.Description = formData["description"];
file.Name = formData["name"] ?? string.Empty;
file.Tags = formData["tags"];
}
file.Resource = thisFile.Data;
//For your PUT, change this to an update.
MyEntities.Entry(file).State = EntityState.Detached;
MyEntities.Files.Add(file);
await MyEntities.SaveChangesAsync();
//return the ID
return Ok(file.Id.ToString());
}
I got the InMemoryMultipartFormDataStreamProvider from this article:
https://conficient.wordpress.com/2013/07/22/async-file-uploads-with-mvc-webapi-and-bootstrap/
And adapted it to fit my needs for the form data I was returning.

DNN - Adding a secure folder

I'm currently coding a module where users can add secure folders.
But the instance method requires a parameter of an instance name, i've no idea what they mean. Could someone explain it to me?
DotNetNuke.Services.FileSystem.SecureFolderProvider.Instance("Test2").AddFolder(txtFolderName.Text, new FolderMappingInfo
{
PortalID = base.PortalId,
MappingName = txtFolderName.Text
});
Any suggestions what i am doing wrong?
With some help of garethbh, i came up with this:
// Get folder mapping
var folderMapping = FolderMappingController.Instance.GetFolderMapping(PortalId, "Secure");
// Add folder and get the result back of the folder information
var folder = FolderManager.Instance.AddFolder(new FolderMappingInfo
{
FolderProviderType = folderMapping.FolderProviderType,
FolderMappingID = 9,
Priority = 2,
PortalID = PortalId,
}, portalFilePath);
This works fine for me.
You need to pass in the name of your folder mapping provider type. If you search for usages of SecureFolderProvider's base class (FolderProvider), you'll see what you need.
Eg:
var folderMapping = FolderMappingController.Instance.GetFolderMapping(PortalId, "Secure");
if (folderMapping != null)
{
SecureFolderProvider.Instance(folderMapping.FolderProviderType).AddFolder(folderPath, folderMapping);
}
I've never actually used the secure folder provider before so I'm just guessing you need the one with the 'Secure' mapping name (but you may want to use 'Database' depending on your needs or create your own folder provider). See the FolderMappings table in the database for available types.
From the DNN wiki http://www.dnnsoftware.com/wiki/Page/Folder-Types and http://www.dnnsoftware.com/wiki/Page/Folder-providers

Simple login for multi-domain intranet?

I have an intranet server on a Windows domain (server is Windows 2003, IIS6, NTFS permissions). It is on the domain Domain01. I have users from two domains in the same forest that access this intranet: Domain01 and Domain02 (DCs also running Windows 2003). Currently, the users are required to login by entering either:
Domain01\username or username#Domain01
My users are completely and thoroughly confused by having to enter the domain each time they log in.
Is there any way to simply allow them to log in by entering just their username and password WITHOUT the domain? For example, have the server try Domain01 by default, and if the login fails to try Domain02?
NOTE: I would like to do this via IIS or server settings if possible, rather than programmatically (for reference, I am using ASP.NET 2.0).
Yes. Usually what I do is do a global catalog search using the supplied user name as the sAMAccountName. Doing this with a PrincipalSearcher requires getting the underlying DirectorySearcher and replacing it's SearchRoot. Once I find the corresponding user object I extract the domain from the user object's path and use that as the domain for the authentication step. How you do the authentication varies depending on what you need it to do. If you don't need impersonation you can use PrincipalContext.ValidateCredentials to make sure that the username/password match using a PrincipalContext that matches the domain of the user account that you previously found. If you need impersonation check out this reference.
// NOTE: implement IDisposable and dispose of this if not null when done.
private DirectoryEntry userSearchRoot = null;
private UserPrincipal FindUserInGlobalContext( string userName )
{
using (PrincipalSearcher userSearcher = new PrincipalSearcher())
{
using (PrincipalContext context
= new PrincipalContext( ContextType.Domain ))
{
userSearcher.QueryFilter = new UserPrincipal( context );
DirectorySearcher searcher
= (DirectorySearcher)userSearcher.GetUnderlyingSearcher();
// I usually set the GC path from the existing search root
// by doing some string manipulation based on our domain
// Your code would be different.
string GCPath = ...set GC path..
// lazy loading of the search root entry.
if (userSearchRoot == null)
{
userSearchRoot = new DirectoryEntry( GCPath );
}
searcher.SearchRoot = userSearchRoot;
using (PrincipalContext gcContext =
new PrincipalContext( ContextType.Domain,
null,
GCPath.Replace("GC://",""))
{
UserPrincipal userFilter = new UserPrincipal( gcContext );
userFilter.SamAccountName = userName;
userSearcher.QueryFilter = userFilter;
return userSearcher.FindOne() as UserPrincipal;
}
}
}
}