This topic explains how to authenticate your cloud server with Windows Push Notification Services (WNS) and receive an access token in return.
Prerequisites
- A working knowledge of tile and notification terms and concepts, as well as XML. This topic also assumes that you know how to create a basic Windows Store app with JavaScript using Windows Runtime APIs.
- Familiarity with push notification and WNS concepts, requirements, and operation as discussed in the push notification overview.
Instructions
Step 1: Register your app with the Dashboard
Before you can send notifications through WNS, you must register your app with the Dashboard, the developer portal that supports the end-to-end process for submitting, certifying, and managing applications for sale in the Windows Store. When you register your app with the Dashboard, you are given credentials—a Package security identifier (SID) and a secret key—which your cloud service will use to authenticate itself with WNS.
To register:
- Go to the Windows Store app development page of the Windows Dev Center and sign in with your Microsoft account.
- Once you have signed in, select the Dashboard tab.
- On the Dashboard, select Submit a new app, shown here.

- On the Submit an app page, shown below, select Name to provide a unique name for your app. Note that you cannot proceed without performing this step first. Enter the name and click the Reserve name button. Once you have successfully reserved a name for your app, the other details become available to modify should you choose to do so at this time.

Step 2: Obtain the identity values for your app
When you reserved a name for your app, the Windows Store created your associated credentials. It also assigned identity values that must be present in your app's manifest file (package.appxmanifest): name and publisher. If you have already uploaded your app to the Windows Store, these values will have automatically been added to your manifest. If you have not uploaded your app, you will need to add the identity values to your manifest manually.
- Select Advanced features, shown here.

- On the "Advanced features" page, select Push notifications and Live Connect services info, shown here.

- On the "Push notifications and Live Connect services info" page, select Identifying your app, shown here.

- Follow the instructions given on the "Identifying your app" page to set the identity values in your app's manifest, either manually or by using Microsoft Visual Studio Express 2012 for Windows 8.
Step 3: Obtain the credentials for your app
- From the "Identifying your app" page, select Authenticating your service, shown here.

- From the "Authenticating your service" page, shown below, retrieve your Package security identifier and client secret. To send notifications to this app, your cloud service must use these credentials exactly. You cannot use the credentials of another cloud service to send notifications to this app and you cannot use these credentials to send notifications to another app.

- Upload the SID and client secret to your cloud server.
Important The SID and client secret should be securely stored and accessed by your cloud service. Disclosure or theft of this information could enable an attacker to send notifications to your users without your permission or knowledge.
Step 4: Send the cloud server's credentials to WNS
The cloud service sends it credentials in an HTTPS authentication request, including the required parameters in the "application/x-www-for-urlencoded" format.
This example shows a sample HTTPS authentication request, including the Package SID and secret key in the list of parameters. Supply your own Package SID in the "client_id" field and your own secret key in the "client_secret" field. See Push notification service request and response headers for syntax information.
POST /accesstoken.srf HTTP/1.1 Content-Type: application/x-www-form-urlencoded Host: https://login.live.com Content-Length: 211 grant_type=client_credentials&client_id=ms-app%3a%2f%2fS-1-15-2-2972962901-2322836549-3722629029-1345238579-3987825745-2155616079-650196962&client_secret=Vex8L9WOFZuj95euaLrvSH7XyoDhLJc7&scope=notify.windows.com
A response of "200 OK" indicates that the authentication was successful and that the response includes an access token for the cloud server to use with any notifications it sends, until that access token expires.
This example shows a sample reply for a successful authentication, including the access token.
HTTP/1.1 200 OK
Cache-Control: no-store
Content-Length: 422
Content-Type: application/json
{
"access_token":"EgAcAQMAAAAALYAAY/c+Huwi3Fv4Ck10UrKNmtxRO6Njk2MgA=",
"token_type":"bearer"
}
This example supplies the code needed to accomplish this step. You can copy this example directly into your code. For your code to use this example properly, you must include these directives:
- using System.Runtime.Serialization.Json;
- using System.Runtime.Serialization;
- using System.IO;
[DataContract] public class OAuthToken { [DataMember(Name = "access_token")] public string AccessToken { get; set; } [DataMember(Name = "token_type")] public string TokenType { get; set; } } private OAuthToken GetOAuthTokenFromJson(string jsonString) { using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString))) { var ser = new DataContractJsonSerializer(typeof(OAuthToken)); var oAuthToken = (OAuthToken)ser.ReadObject(ms); return oAuthToken; } } protected OAuthToken GetAccessToken(string secret, string sid) { var urlEncodedSecret = HttpUtility.UrlEncode(secret); var urlEncodedSid = HttpUtility.UrlEncode(sid); var body = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", urlEncodedSid, urlEncodedSecret); string response; using (var client = new WebClient()) { client.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); response = client.UploadString("https://login.live.com/accesstoken.srf", body); } return GetOAuthTokenFromJson(response); }
Related topics
- Push notifications overview
- Push notification service request and response headers
- Push and periodic notifications sample
- Quickstart: Sending a push notification
- How to request, create, and save a notification channel
Build date: 11/29/2012