Monday, July 20, 2015

Visual Studio 2015 and .NET 4.6 Available for Download

Few minutes back Microsoft announced the release of Visual Studio 2015 and .Net 4.6.

Microsoft has added tremendous amount of new features in to the new version of VS. According to "Soma" Somasegar, Microsoft’s corporate VP of its Developer Division, “When you think about Visual Studio, the core theme is that VS 2015 is a cross-platform tool set". I'm so existed to test the product, specially the cross platform features.

Soma's blog on VS 2015.





You can see the Visual Studio release note from  here.


Tuesday, July 7, 2015

Deleting a Site Collection Forcefully

Before writing about the solution, let me brief the problem. I was working for one my clients, suddenly I got an error message inside the PowerShell window. I was trying to move a Site Collection using the Move-SPSite cmdlet to a new content database. "A transport-level error has occurred when receiving results from the server" was the error. This is not what I'm going to talk about as I'm still trying to rectify the cause for the same.



Just after the above error, the existing site collection stopped working. Once I select the Site Collection in the "View All Site Collections" in central admin, on the right side of the screen, all I saw was a empty values about the site collection. Even it was without a Content Database. My plan was to delete the site collection and restore the backup. But deleting failed due to the unavailability of the database. Even the Remove-Spsite cmdlet couldn't help me as it can only delete a full provisioned Site Collection. But the below approach worked well.

$siteUrl = “http://mydomain/Site“
$site = get-spsite $siteUrl
$siteId = $site.Id
$siteDatabase = $site.ContentDatabase
$siteDatabase.ForceDeleteSite($siteId, $false, $false)

Thanks to Peter.


Friday, May 1, 2015

Windows Live Writer with Blogger and 2 factor Authentication

I’m a fan of Windows Live Writer and for me that’s the best blogging tool I’ve ever used. Recently I enabled 2-factor authentication in my email account. When I try to log in to my blogger account through Live Writer, it says either the username or the password is wrong.

This is an easy fix. Log in to your Gmail within the browser. Go to your account on top right hand corner and click “My Account’'. Next click “Sign-in & Security”. Once you are in, you will see “App passwords”, click on it.

You need to generate a App Password for your Live Writer and that will be your password for Live Writer from today onwards to log in to Blogger. Once the password is generated, use it in your Live Writer with your login name.

Blog happily and securely!

Sunday, September 7, 2014

Building Enterprise Applications on Azure

I was invited by Redynamics Sri Lanka to conduct a technical session on “Building Enterprise Application on Azure” for Microsoft FSI sector.

Saturday, August 9, 2014

A career in SharePoint – University of Moratuwa

I was invited by 2nd year degree students of University of Moratuwa, IT faculty to conduct a session on pursuing a career in SharePoint. Though I have done the same session previously, I’m honored to conduct a session at UOM.

Sunday, July 20, 2014

A career in SharePoint – ESoft Metro Campus

I was invited by Microsoft Sri Lanka to conduct a session for degree students at ESoft Metro Campus Colombo on pursing a career in SharePoint. Session was scheduled on 18th July 2014.

Thursday, April 10, 2014

Create a Provider Hosted App (High-trust) for SharePoint

SharePoint 2013 introduced the SharePoint App model for developers. Out of the three app models, SharePoint Hosted, Auto Hosted and Provider Hosted I’m creating and explaining the step by step approach to develop a simple Provider Hosted (High-trust) app in this post. This is not an introduction to SharePoint App model but an introduction to Provider Hosted (PH) Apps.

Diagram below denote my dev environment.
 image

One VM for AD and DNS and the second VM hosts SharePoint 2013 and SQL 2012.

There are two types of PH Apps. Apps that can be hosted in Office 365 environments and the PH Apps that can be only hosted in On-Premises environments which are also called as High-trust apps.

Provider Hosted app consists of an App deployed in to SharePoint and a separately deployed web application. In a production environment the web application is normally deployed in a separate server or a separate server farm.
image

The reason why high-trust apps are only compatible with On-Premises environments is, it uses a certificate instead of a context token (OAuth) to make the trust between the two servers. So these apps require some sort of configuration on the SharePoint server as well as the remote server hosting the web application. SharePoint includes server-to-server security token service (STS) which provides access tokens for server-to-server authentication. The server-to-server STS enables temporary access tokens to access other application services including apps for SharePoint 2013. To establish a trust relationship between the SP Server and App we use a X.509 certificate and a few PowerShell cmdlets.

We need to create a .pfx file and a corresponding .cer file. First file which is the pfx, contains the private key which will be used by the remote web application to encrypt it’s communication to SharePoint server. The .cer file contains the public key which will be used by SharePoint server to decrypt the messages and to verify those messages come from the same remote web application. Also to verify that the remote web application has an access token from a token issuer that SharePoint trusts which in this case the certificate.

For the demonstration purposes I will use a self signed certificate instead of a certificate issued by a certificate authority.

Create and export a Test Certificate

Open IIS Manager in the SharePoint server and select server certificates.  
Screen Shot 2014-04-10 at 10.41.20 AM 

Click Create a self-signed certificate.
Screen Shot 2014-04-10 at 10.45.05 AM

Name the certificate. Mine is ProviderHostedHighTrust.
Screen Shot 2014-04-10 at 10.48.05 AM

As the next step, right click the certificate created, export it to a folder providing a password. It will create a file with a pfx extension.
Screen Shot 2014-04-10 at 11.05.02 AM

If your dev environment is not similar to mine and if you have a separate server to host the remote web application, please move the pfx file to the same.

Create the .cer file

Go to IIS Manager and open Server Certificates.
Screen Shot 2014-04-10 at 11.46.31 AM
On the details tab, click Copy to file, where it opens the certificate export wizard. Click next and move forward with the default option, “No, Do not export the private key”.
Screen Shot 2014-04-10 at 11.50.17 AM

Click next with default options and save the certificate.
Screen Shot 2014-04-10 at 11.54.50 AM Screen Shot 2014-04-10 at 11.55.56 AM Now we are done with certification creation & exporting part. But we have to make sure STS application pool identity as well as SharePoint Web Application; application pool identity have read permission to the location of the .cer file.

Configure SharePoint 2013 Server to use the Certificate and trust the App hosted in Remote Server.

What I have explained below is suited for a dev environment and NOT for a Production nor a staging server.
In the SharePoint Server, open the SharePoint 2013 Management Shell with Run as Administrator. Execute the cmdlets below.

Create a certificate Object

$publickeyPath = “C:\Certificates\ProviderHostedHighTrust.cer”


$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($publickeyPath)




Ensure that SharePoint treats the certificate as a root authority




New-SPTrustedRootAuthority -Name "ProviderHostedHighTrust" -Certificate $certificate




Screen Shot 2014-04-10 at 3.09.30 PM 
Get the ID of the authorization realm.




$realm = Get-SPAuthenticationRealm




To access data in SharePoint, my remote web application needs a access token, which is issued by a token issuer that SharePoint trusts.  As I’ve mentioned above the certificate is the token issuer.

Next step is a very important. If we look in to a production environment, each certificate is issued by a unique issuer which is represented by a GUID. A limitation of SharePoint, make sure any letters in GUID must be lower case.  But in my dev environment I’m using the same certificate for all Provider hosted high-trust app.




$specificIssuerId = "d250d0bc-d44e-4d8b-9e36-567817943628"


$fullIssuerIdentifier = $specificIssuerId + '@' + $realm




Now is the time to make the certificate a trusted token issuer.




New-SPTrustedSecurityTokenIssuer -Name "High Trust Demo Certificate" -Certificate $certificate -RegisteredIssuerName $fullIssuerIdentifier –IsTrustBroker




In the above cmdlet I have used a friendly name which is not a common scenario in a production environment. The reason is, name parameter must be unique, so its better to add a GUID as part of the name. You will see an output similar to below screen.

Screen Shot 2014-04-10 at 4.09.12 PM



Next do an iisreset command to register the token issuer immediately.


In a dev environment we have another issue to be solved. SharePoint does NOT accept self-signed certificates. So we need to turn off SharePoint's normal requirement that HTTPS be used when remote web applications call into SharePoint or else you will see a 403(forbidden) message. Turning off HTTPS is not recommended as form there onwards all traffic from the Remote Web Application to SharePoint wont be encrypted. But with a self-signed certificate that’s the only option for now.





Allowing OAuth over HTTP




$serviceConfig = Get-SPSecurityTokenServiceConfig


$serviceConfig.AllowOAuthOverHttp = $true


$serviceConfig.Update()




OK, Lets build the App in Visual Studio. I’m on VS 2013. You can use VS 2012 as well.



Screen Shot 2014-04-10 at 5.08.29 PM



Select “provider hosted” and provide a URL for debug purposes. For the remote web application I have selected Web Forms, if your prefer MVC, feel free to go ahead. Next comes the “Configure Authentication” screen. Select “use a certificate”. Values you provide here will be written to the Web.Config of the remote web application.



For the certificate location, browse and select the .pfx file you created in a previous step. Provide the password. And the issuer id is the GUID with lowercase letters. Mine is d250d0bc-d44e-4d8b-9e36-567817943628.

Screen Shot 2014-04-10 at 5.30.22 PM



Click finish and VS creates two projects for you. One, the App which will deployed to SharePoint and the second, the remote web application. You do not have to write any code, as the template have some code already in the Default.aspx.cs. But I added one more line. :)

Screen Shot 2014-04-10 at 5.58.48 PM





Good luck. Hit F5. It' asks you to Trust the app. Go ahead and trust it. Hurray!! If you followed me properly, you should get a screen similar to below.

Screen Shot 2014-04-10 at 6.03.25 PM



As you can see, below are the information written to the Web.Config file.

  <appSettings>

    <
add key="ClientId" value="307490ca-d53b-4dcf-81d4-e7c4bdffb384" />

    <
add key="ClientSigningCertificatePath" value="C:\Certificates\ProviderHostedHighTrust.pfx" />

    <
add key="ClientSigningCertificatePassword" value="password" />

    <
add key="IssuerId" value="d250d0bc-d44e-4d8b-9e36-567817943628" />

  </
appSettings>




What you need to remember at this point is, still we are on a self-signed certificate and HTTP instead of HTTPS. So still you test your app with a domain signed or commercial certificate on HTTPS, your app is not ready for release. I’ll discuss more on domain signed certificates on another blog post.