2011-04-25 49 views
2

我有一個ASP.NET MVC應用程序,我試圖將DotNetOpenAuth用於Google OAuth。我正在使用示例中的GoogleConsumer類,並嘗試執行身份驗證的第一步。下面的代碼是基本相同的是,在提供的WebForms應用程序,只是在MVC控制器:DotNetOpenAuth WebConsumer不會重定向到ASP.NET MVC應用程序中的OAuth提供程序

public string Authenticate() 
{ 
    GoogleTokenManager tokenManager = new GoogleTokenManager(ConsumerKey, ConsumerSecret); 
    WebConsumer webConsumer = new WebConsumer(GoogleConsumer.ServiceDescription, tokenManager); 
    GoogleConsumer.RequestAuthorization(webConsumer, GoogleConsumer.Applications.Gmail); 
    return ""; 
} 

當我提出一個AJAX請求到控制器中的代碼執行,但我從來沒有重定向到谷歌網頁進行驗證。

回答

3

底層請求正在返回一個302重定向響應,我沒有正確處理。我發現更有幫助的是指定我的控制器中的另一個動作的回調URL,如下所示:

public ActionResult Authenticate() 
{ 
    string callbackUrl = Request.Url.ToString().Replace("Authenticate", "OtherAction"); 
    Uri callback = new Uri(callbackUrl); 

    WebConsumer webConsumer = new WebConsumer(GoogleConsumer.ServiceDescription, TokenManager); 
    Dictionary<string, string> extraParameters = new Dictionary<string, string>(); 
    extraParameters.Add("scope", GoogleConsumer.GetScopeUri(GoogleConsumer.Applications.Gmail)); 

    UserAuthorizationRequest request = webConsumer.PrepareRequestUserAuthorization(callback, extraParameters, null); 
    return webConsumer.Channel.PrepareResponse(request).AsActionResult(); 
} 

public ActionResult OtherAction() 
{ 
    // oauth_verifier, oauth_token are now in the RequestQueryString 
} 
相關問題