2011-03-18 35 views
3

當我打電話給我的身份驗證時,我從查詢字符串傳遞返回URL。當Open Id提供者重定向回到同一個Action Result時,Return Url參數爲空。在呼叫中堅持這一點的最好方法是什麼?ASP.NET MVC DotNetOpenAuth在Authenticate之後得到ReturnURL?

是否有人在會話中存儲本地返回URL?下面是有問題的方法。

[ValidateInput(false)] 
    public ActionResult Authenticate(string returnUrl) 
    { 
     openId = new OpenIdRelyingParty(); 

     IAuthenticationResponse response = openId.GetResponse(); 

     if (response == null) 
     { 
      Identifier id; 
      if (Identifier.TryParse(Request.Form["openid_identifier"], out id)) 
      { 
       try 
       { 
        // at this point we have a return Url 
        return openId.CreateRequest(id).RedirectingResponse.AsActionResult(); 
       } 
       catch (ProtocolException pex) 
       { 
        ModelState.AddModelError("", pex.Message); 
        return View("LogOn"); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("", "Invalid Identifier"); 
       return View("LogOn"); 
      } 

     } 
     else 
     { 
      switch (response.Status) 
      { 
       case AuthenticationStatus.Authenticated: 
        FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, true); 
        // at this point return URL is null 

        var fetch = response.GetExtension<FetchResponse>(); 
        string email = string.Empty; 
        if (fetch != null) 
         email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email); 

        if (!string.IsNullOrEmpty(returnUrl)) 
        { 
         var test = FormsAuthentication.GetRedirectUrl(User.Identity.Name, false); 
         var url = AppHelper.GenerateReturnURL(Request, returnUrl); 
         return Redirect(url); 
        } 
        else 
        { 
         return RedirectToAction("Index", "Home"); 
        } 
       case AuthenticationStatus.Canceled: 
        ModelState.AddModelError("", "Canceled at provider"); 
        return View("LogOn"); 
       case AuthenticationStatus.Failed: 
        ModelState.AddModelError("", response.Exception.Message); 
        return View("LogOn"); 
      } 
     } 

     return View("LogOn"); 
    } 
+0

你肯定被填充RETURNURL值當你的表格最初發布到你的身份驗證行動,OpenID提供任何調用之前? – 2011-03-18 17:34:17

+0

積極。它充滿了/管理,因爲我期望從查詢字符串。當它返回到相同的動作時,它是空的。你會期望它在OpenID提供者調用之後保持不變嗎? – aherrick 2011-03-18 17:38:04

+1

是否將returnUrl作爲URL查詢字符串而不是字段提交提交?例如

,如果是的話我會期待它被重新填充。 – 2011-03-18 18:15:26

回答

5

我想通了:

     //add returnURL as a callback argument 
        if (!string.IsNullOrEmpty(returnUrl)) 
         request.AddCallbackArguments("returnUrl", returnUrl); 
+2

是的,這是一個合理的方法。 – 2011-03-19 17:02:07