【问题标题】:MVC4 oAuth causing null value exception: Provider is nullMVC4 oAuth 导致空值异常:提供者为空
【发布时间】:2013-10-08 02:13:29
【问题描述】:

我的问题与这篇文章类似。 MVC4 oAuth causing null value exception

但是,我没有将按钮更改为图像输入。调试时,值都存在于为 OAuth 提供程序创建按钮的 for each 循环中。这里:

@model ICollection<AuthenticationClientData>

@if (Model.Count == 0)
{
  <div class="message-info">
    <p>There are no external authentication services configured. See <a href="http://go.microsoft.com/fwlink/?LinkId=252166">this article</a>
    for details on setting up this ASP.NET application to support logging in via external services.</p>
  </div>
}
else
{
  using (Html.BeginForm("ExternalLogin", "Account", new { ReturnUrl = ViewBag.ReturnUrl }))
  {
    @Html.AntiForgeryToken()
    <fieldset id="socialLoginList">
      <legend>Log in using another service</legend>
      <p>
      @foreach (AuthenticationClientData p in Model)
      {
        <button type="submit" name="provider" value="@p.AuthenticationClient.ProviderName" title="Log in using your @p.DisplayName account">@p.DisplayName</button>
      }
      </p>
    </fieldset>
  }
}

是否有任何其他情况会导致提供程序返回 null 并引发错误?

编辑:事实上,经过更多调试后,我发现提供程序字符串没有回发到帐户控制器中的此方法中

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
    return new ExternalLoginResult(provider, Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
}

有人知道为什么或如何发生这种情况吗?如果您需要更多代码,请询问。

编辑:这是视图中存在的值的图像,但在帖子中缺失。欢迎提出任何建议。

编辑:为登录页面表单添加了整个视图。提交按钮时没有提交provider值,这在fiddler post中得到确认

编辑:如果我在按钮上添加一个类,然后在名为 provider 的表单中添加一个隐藏字段。然后我可以使用 jquery 设置它的值。然后将该值与表单一起提交,并且确实有效。

<input type="hidden" value="" name="provider" id="provider" />
@foreach(.... etc
<button class="test" type="submit" name="provider" value="@p.AuthenticationClient.ProviderName" title="Log in using your @p.DisplayName account">@p.DisplayName</button>

然后

<script>
    $(function () {
        $('.test').click(function () {
            alert($(this).val());
            $('#provider').val($(this).val());
            return true;
        });
    });
</script>

如果有人能告诉我这是为什么以及如何正确修复它,那就太棒了!

【问题讨论】:

    标签: c# asp.net-mvc-4 oauth


    【解决方案1】:

    在提交表单时执行的任何错误客户端脚本或禁用提交按钮(在您的情况下为外部提供程序按钮)的脚本都将导致空值。确保将 ExternalLogin 提供程序表单发布到其 ActionResult 中,没有任何异常。

    【讨论】:

    • 我没有遇到任何异常,也没有 javascript 错误或拦截帖子的客户端脚本。
    • Fiddler (fiddler2.com) 可能有助于调试。检查提交流量。
    • 我在帖子中看不到提供者属性。我不知道为什么......它只是不存在。它应该是。存在另一个参数 Return url。我将更新原件以包含整个表单对象,以便您可以在原地看到它。
    猜你喜欢
    • 2013-04-22
    • 2015-11-01
    • 2017-10-17
    • 2020-05-11
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    相关资源
    最近更新 更多