Last week, I had a requirement to authenticate users. I did search Google but all the solutions support Owin and related libs, MVC etc.There is no solution for Web form application with .NET 4.0.
For this, you have to follow these steps -
Create application using Microsoft account at this URL.
- Mention Redirect URL where you want to hit after authentication.
- Copy AppId that will help us later.
- In Login.aspx, create a button, and onclick of this button, write the following code.
- <input type="button" ID="BtnSignInMicrosoft" Class="bofficesignin" runat="server" value="Sign in with Office 365" onclick="RedirectToOffice(); return false;" /> and javascript code function RedirectToOffice() { var OfficeRedirectUrl = "
- <%=ConfigurationManager.AppSettings("YourRediectURL mention on application creation")%>"; var Office365ClientId = "
- <%=ConfigurationManager.AppSettings("Office365ClientId")%>"; var OfficeUrl = "https:
Write the following code on your redirect page.
- If Request.Form.Item("id_token") Is Nothing = False Then
- Dim objWebClient As New System.Net.WebClient
- Dim HeaderData As New WebHeaderCollection
- Dim jSerializer As New JavaScriptSerializer
- Dim ResponseString As String = String.Empty
- Dim ResponseData As Dictionary(Of String, String)
- Dim Email As String = String.Empty
- Dim TokenID As String = Request.Form.Item("id_token")
- Dim AccessToken As String = Request.Form.Item("access_token")
- TokenID = TokenID.Substring(0, TokenID.IndexOf("."))
- HeaderData("Authorization") = "Bearer " + AccessToken
- ' Dim objOutlookSetting As OutLookSetting = Serializar.Deserialize(Of OutLookSetting)(BLL.SettingManager.GetValueByKey("EAAdminOutlookSetting"))
- HeaderData("client-request-id") = ConfigurationManager.AppSettings("Office365ClientId")
- HeaderData("return-client-request-id") = "true"
- objWebClient.Headers = HeaderData
- ResponseString = objWebClient.DownloadString("https://outlook.office.com/api/v2.0/Me")
- ResponseData = jSerializer.Deserialize(Of Dictionary(Of String, String))(ResponseString)
- Email = ResponseData("EmailAddress")
And, the rest of the flow will remain the same in your application.