· 9 years ago · Sep 01, 2016, 01:04 PM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.UI;
6using System.Web.UI.WebControls;
7using System.Net;
8using Newtonsoft.Json.Linq;
9using System.Net.Http;
10
11using System.Net.Mail;
12using System.Threading.Tasks;
13using System.Text;
14using System.IO;
15using System.Net.Http.Headers;
16
17
18namespace WebApplication1
19{
20public partial class _Default : Page
21{
22 protected async void Page_Load(object sender, EventArgs e)
23 {
24 if (Request.QueryString["code"]!=null)
25 {
26
27 HttpClient authClient = new HttpClient();
28 string oauthToken, serviceUrl;
29 string sfdcConsumerKey = "";
30 string sfdcConsumerSecret = "";
31 string sfdcUserName = "";
32
33 string sfdcPassword = "";
34 string sfdcToken = "";
35
36 string loginPassword = sfdcPassword + sfdcToken;
37
38 string code = (string)(Request.QueryString["code"]);
39
40 HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>
41 {
42 {"grant_type","authorization_code"},
43 {"client_id",sfdcConsumerKey},
44 {"client_secret",sfdcConsumerSecret},
45 {"username",sfdcUserName},
46 {"password",loginPassword},
47 {"redirect_uri","http://localhost:1474/Default.aspx"}
48 }
49 );
50 HttpResponseMessage message = await authClient.PostAsync("https://test.salesforce.com/services/oauth2/token", content);
51
52 string responseString = await message.Content.ReadAsStringAsync();
53 JObject obj = JObject.Parse(responseString);
54 oauthToken = (string)obj["access_token"];
55 serviceUrl = (string)obj["instance_url"];
56
57 }
58 }
59 protected async void GetToken_Click(object sender, EventArgs e)
60 {
61 await SendResponseAsync();
62 }
63 public async Task SendResponseAsync()
64 {
65
66 string sessionId = String.Empty;
67
68 //////// Working Authorization
69
70 HttpClient authClient = new HttpClient();
71
72 //set OAuth key and secret variables
73 string sfdcConsumerKey = "";
74 string sfdcConsumerSecret = "";
75 //set to Force.com user account that has API access enabled
76 string sfdcUserName = "";
77 string sfdcPassword = "";
78 string sfdcToken = "";
79
80 //create login password value
81 string loginPassword = sfdcPassword + sfdcToken;
82
83 HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>
84 {
85 {"response_type","code"},
86 {"client_id",sfdcConsumerKey},
87 {"redirect_uri","http://localhost:1474/Default.aspx"}
88 }
89 );
90
91 HttpResponseMessage message = await authClient.PostAsync("https://test.salesforce.com/services/oauth2/authorize", content);
92
93 string responseString = message.RequestMessage.RequestUri.ToString();
94 authlink.NavigateUrl = responseString;
95 authlink.Visible = true;
96
97
98 }
99
100
101 }
102}