Tag Archives: C#
VIDEO – Protect WebAPI data endpoint with ADAL (Azure AD)
Wanted to record brief demo of how to protect WebAPI data endpoint with ADAL (Azure Active Directory) Authentication Library. Azure Portal enables us to register custom SPA (Single Page Application) for secure API calls to backend REST data sources. OAuth Bearer HTTP headers are applied to provide security with JWT tokens (JSON Web Token). Video shows all steps from Azure Portal registration to F12 validation of REST data calls.
Video
Screenshots
Code
References
VIDEO – Online WebAPI Generator
I created an online service at https://spjeff.azurewebsites.net/ which generates a MVC WebAPI 2.2 project with the name you enter. A custom ZIP file is generated server side with your custom name for the Project, Namespaces, Assembly, and sent to the browser for download. Best practices are already enabled such as:
- [CORS] decorator
- [Authorize] decorator
- Minimal packages and dependency
- Zero MVC boilerplate
- Lean mean API ready for dev & prod
From there double click SLN and begin coding. Enjoy! ![]()
Video
Online WebAPI Generator from Jeff Jones on Vimeo.
Screenshots
Post to Newsfeed on behalf of another user (Server Object Model)
The below code will post to Newsfeed for any user account you specify. Activity appears the same as if the user manually posted. This could be helpful for populating Newsfeed from external activity, custom event receivers, workflow, and other developer sources. Enjoy! ![]()
Screenshot
Code
using Microsoft.Office.Server.Social;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Config
string siteUrl = "http://mysite";
string login = "i:0#.w|domain\\user";
using (SPSite site = new SPSite(siteUrl))
{
// Context
SPUser user = site.RootWeb.SiteUsers[login];
SPUserToken token = user.UserToken;
SPServiceContext ctx = SPServiceContext.GetContext(site);
using (new SPServiceContextScope(ctx))
{
// Init
UserProfileManager upm = new UserProfileManager(ctx);
UserProfile prof = upm.GetUserProfile(user.LoginName);
SPSocialFeedManager mgr = new SPSocialFeedManager(prof, ctx, token);
SPSocialPostCreationData post = new SPSocialPostCreationData();
// Text
post.ContentText = "hello world, look at {0}!";
post.UpdateStatusText = true;
// Link
SPSocialDataItem [] link = new SPSocialDataItem [1];
link[0] = new SPSocialDataItem();
link[0].ItemType = SPSocialDataItemType.Link;
link[0].Text = "My Cool Link";
link[0].Uri = new Uri("http://www.google.com");
post.ContentItems = link;
// Save
SPSocialThread thread = mgr.CreatePost(null, post);
}
}
}
}
}
