【问题标题】:MVC4 oAuth causing null value exceptionMVC4 oAuth 导致空值异常
【发布时间】:2013-04-22 06:01:32
【问题描述】:

我正在尝试允许用户使用他们的 Facebook、Twitter、Google 或 Microsoft 帐户登录到我的 MVC4 站点。页面加载正常,但是当单击其中一个按钮以使用其中一项服务进行身份验证时,出现错误。

System.ArgumentNullException:值不能为空。

错误是指帐户控制器中的这行代码,因为Provider 为空

OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl);

这行代码属于这个控制器动作

    internal class ExternalLoginResult : ActionResult
    {
        public ExternalLoginResult(string provider, string returnUrl)
        {
            Provider = provider;
            ReturnUrl = returnUrl;
        }

        public string Provider { get; private set; }
        public string ReturnUrl { get; private set; }

        public override void ExecuteResult(ControllerContext context)
        {
            OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl);
        }
    }

当页面加载时,每个按钮的值都按预期设置。已编译页面的页面源显示名称和值集。在这个例子中,对于 twitter

    <div class="authprovider">
        <input type="image" name="Provider" value="twitter" alt="Sign in using Twitter" src="/Images/twitter.png" title="Twitter" />
    </div>

在 AuthConfig 文件中的 RegisterAuth() 下,twitter 部分如下所示

OAuthWebSecurity.RegisterTwitterClient(
    consumerKey: "*Hidden*",
    consumerSecret: "*Hidden*",
    displayName:"Twitter",
    extraData:new Dictionary<string, object>{{"Icon","/Images/twitter.png"}});

在名为 _ExternalLoginsListPartial 的局部视图中,用于输出按钮的代码如下所示

    @foreach (AuthenticationClientData p in Model)
    {
        <div class="authprovider">
            <input type="image" name="Provider" value="@p.AuthenticationClient.ProviderName" alt="Sign in using @p.DisplayName" src="@p.ExtraData["Icon"].ToString()" title="@p.DisplayName" />
        </div>

    }

我已将输入类型更改为image 以允许使用图像按钮。默认情况下,它的类型为submit。所有 4 个外部登录选项都会发生此错误。希望其他人遇到过它,到目前为止,这让我摸不着头脑。

[编辑 15:41]

将按钮改回提交似乎已通过它,但我真的不想要一个可怕的按钮而不是图像。

【问题讨论】:

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


    【解决方案1】:

    好吧,事实证明&lt;input type='image' 返回的是点击图像的坐标,而不是指定的值。

    我发现的一个解决方法是将输入更改为一个按钮,该按钮封装如下图像。

    <div class="authprovider">
        <button type="submit" name="Provider" value="@p.AuthenticationClient.ProviderName"><img alt="Sign in using @p.DisplayName" src="@p.ExtraData["Icon"].ToString()" title="@p.DisplayName"/></button>
    </div>
    

    然后添加样式以移除按钮颜色等

    .authprovider button {
        background-color: #fff;
        border: none;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-08
      • 2020-05-11
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 2017-10-17
      • 2015-09-12
      相关资源
      最近更新 更多