May 25, 2018 by Anuraj Estimated read time : 9 mins
ASP.NET CoreYammerOAuth 2
This post shows you how to enable your users to sign in with their Yammer account. Similar to the other social networks, the authentication is an OAuth 2 flow, beginning with the user authenticating with their Yammer credentials. The user then authorizes your app to connect to their Yammer network. The end result is a token that your app will use to write events to Yammer and retrieve Yammer data.
Make sure the redirect URL is https://localhost:5001/signin-yammer. Once the app is created, you will be redirected to a page like this
Where you will be able to see the client id and client secret. Next you need to create an ASP.NET Core MVC project, I am using OAuth provider for authentication. So here is the code for enabling OAuth authentication in ASP.NET Core.
And here is the code to show authentication dialog.
Now you can run the app, click on the Login button, which will redirect to Yammer Authentication dialog.
Once you provide the credentials and click Login, you will be redirected to the authorization dialog, like this.
Once you click Ok, you will be redirected back to your application and you can get details about user from current user endpoint, using following code.
But I got an error like this instead of getting the call in OnCreatingTicket event.
From the stack trace I am able to find that, it is thrown from OAuthTokenResponse class constructor, which is getting created in ExchangeCodeAsync method. Since ASP.NET Core is Open Source (Thank you Microsoft), I am able to look into the source code and I found Yammer response is different from the other providers, so there is no other option than writing custom authentication provider for Yammer. So I wrote a Yammer provider, which inherits from OAuthHandler class. And I am overriding the ExchangeCodeAsync so that it the proper JObject can be available for the OAuthTokenResponse constructor. So here is the implementation.
And here is my updated ConfigureServices method.
Now it is getting redirected back to our app, when authorization is completed, and on OnCreatingTicket event, I am updating the identity with userId and full name, so that we can use it in the App. Here is the screenshot of the app running on my machine.
Source code of the application is available in Github