ASP.Net网页C#的销售人员身份验证

时间:2011-08-24 19:52:36

标签: c# asp.net oauth salesforce

我正在使用OAuth 2.0通过销售人员对我的网站进行身份验证。我下载并运行了Java示例代码并且运行正常。现在我需要从ASP.Net网页上做同样的事情,但它似乎不起作用。当我执行最后的POST以获取访问令牌时,它会给我一个错误System.Net.WebException:远程服务器返回错误:(400)错误请求。

我使用的链接here中提到的httppost方法相同。有关详细信息,发布该帖子的代码如下。

public partial class Authenticate2 : System.Web.UI.Page
{
    private string tokenUrl;
    protected void Page_Load(object sender, EventArgs e)
    {
        Logger.LogInfoMessage("I am Authenticate 2");
        string code = Request["code"];
        Logger.LogInfoMessage("Code is: " + code);
        string parameters = "code=" + code
            + "&grant_type=authorization_code"
            + "&client_id=" + Authenticate.CONSUMER_KEY
            + "&client_secret=" + Authenticate.CONSUMER_SECRET
            + "&redirect_uri" + Authenticate.REDIRECT_URL;
        string result = HttpPost(tokenUrl, parameters);
        Logger.LogInfoMessage(result);
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        tokenUrl = Authenticate.ENVIRONMENT + "/services/oauth2/token";
    }
    public string HttpPost(string URI, string Parameters)
    {
        System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";

        // Add parameters to post
        byte[] data = System.Text.Encoding.ASCII.GetBytes(Parameters);
        req.ContentLength = data.Length;
        System.IO.Stream os = req.GetRequestStream();
        os.Write(data, 0, data.Length);
        os.Close();

        // Do the post and get the response.
        System.Net.WebResponse resp = req.GetResponse();
        if (resp == null) return null;
        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        return sr.ReadToEnd().Trim();
    }
}

authenticate类只是根据这个重定向:

public partial class Authenticate : System.Web.UI.Page
{
    public const string CONSUMER_KEY = "XXX";
    public const string CONSUMER_SECRET = "XXX";
    public  const string REDIRECT_URL = "https://localhost/authenticate2.aspx";
    public  const string ENVIRONMENT = "https://test.salesforce.com";

    public  const string ACCESS_TOKEN = "ACCESS_TOKEN";
    public  const string INSTANCE_URL = "INSTANCE_URL";
    public  const string INVOICE_ID = "invoiceId";

    private string authenticateUrl = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        Logger.LogInfoMessage("I am in Authenticate");
        string accessToken = (string)Session[ACCESS_TOKEN];
        Logger.LogInfoMessage("Access Token is:" + accessToken);
        if (accessToken == null)
        {
            Logger.LogInfoMessage("Redirecting to:" + authenticateUrl);
            Response.Redirect(authenticateUrl);
            return;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        authenticateUrl = ENVIRONMENT
                + "/services/oauth2/authorize?response_type=code&client_id="
                + CONSUMER_KEY + "&redirect_uri="
                + HttpUtility.UrlEncode(REDIRECT_URL, System.Text.Encoding.UTF8);
    }
}

我得到的错误的屏幕截图如下:

The Screen Shot of the error

3 个答案:

答案 0 :(得分:2)

构建参数字符串时,需要对每个参数值进行URL编码。您还可以捕获WebException并阅读响应主体,该主体还将提供有关该问题的更多信息。

答案 1 :(得分:0)

您的HTTPPost参数构造不正确,这就是为什么请求被拒绝"错误的请求":

string parameters = "code=" + code
            + "&grant_type=authorization_code"
            + "&client_id=" + Authenticate.CONSUMER_KEY
            + "&client_secret=" + Authenticate.CONSUMER_SECRET
            + "&redirect_uri" + Authenticate.REDIRECT_URL;

你错过了" ="在这里:+ "&redirect_uri = " + Authenticate.REDIRECT_URL;

答案 2 :(得分:0)

有Salesforce API的oAuth实现的javascript版本,但您可以使用它来了解身份验证流程: https://www.youtube.com/watch?v=ULWBdjJx1Ss

该视频描述了获取令牌并使用它来获取数据所需的STEP BY STEP过程。