ASP.NET MVC DotNetOpenAuth在Authenticate之后得到ReturnURL?

ahe*_*ick 3 asp.net-mvc dotnetopenauth

当我调用我的身份验证时,我将从查询字符串传递返回URL.当Open Id提供程序重定向回相同的Action Result时,Return Url参数为null.在电话会议中坚持这个的最佳方法是什么?

人们是否在会话中存储了本地Return 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");
    }
Run Code Online (Sandbox Code Playgroud)

ahe*_*ick 5

我想到了:

                        //add returnURL as a callback argument
                    if (!string.IsNullOrEmpty(returnUrl))
                        request.AddCallbackArguments("returnUrl", returnUrl);
Run Code Online (Sandbox Code Playgroud)

  • 是的,这是一种合理的方法. (2认同)