MVC4 oAuth causing null value exception

陌路散爱 提交于 2020-01-14 05:46:31

问题


I am trying to allow users to log in to my MVC4 site using their Facebook, Twitter, Google or Microsoft accounts. The page loads fine but when clicking one of the buttons to authenticate with one of these services, I get an error.

System.ArgumentNullException: Value cannot be null.

The error refers to this line of code in the Account Controller because Provider is null

OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl);

This line of code belongs to this controller action

    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);
        }
    }

When the page loads, the value for each button is being set as expected. The page source for the compiled page shows the name and value set. In this example, for twitter

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

Under RegisterAuth() in the AuthConfig file the twitter section looks like this

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

In the partial view called _ExternalLoginsListPartial the code for outputting the button looks like this

    @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>

    }

I have changed the input type to image to allow for the image button. By default it is of type submit. This error happens for all 4 of the extenal login options. Hoping someone else has come across it, it has left me scratching my head so far.

[edit 15:41]

Changing the button back to submit seems to get passed it, but I don't really want a horrible button rather than the image.


回答1:


Well, it turns out the <input type='image' returns the coordinates of the image clicked, not the specified value.

a work around I found is changing the input to a button, which encapsulates an image as below.

<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>

then add a style to remove the button colour etc

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


来源:https://stackoverflow.com/questions/16263938/mvc4-oauth-causing-null-value-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!