MVC4的OAuth造成空值异常 [英] MVC4 oAuth causing null value exception
问题描述
我想允许用户使用他们的Facebook,Twitter的,谷歌或微软帐户登录到我的网站MVC4。在页面加载罚款,但点击一个按钮,当与这些服务之一进行身份验证,我得到一个错误。
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:值不能为空的
该错误是指账户控制器此行code,因为提供
为空
The error refers to this line of code in the Account Controller because Provider
is null
OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl);
code这行属于这个控制器动作
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>
在 RegisterAuth()
在AuthConfig文件Twitter的部分看起来像这样
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"}});
在所谓的_ExternalLoginsListPartial code输出该按钮的局部视图看起来像这样
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>
}
我已经改变了输入类型为图片
来允许图像按钮。默认情况下它的类型是提交
的。对于发生的extenal登录选项全部4此错误。其他希望有人碰到过它,它给我留下抓我的头至今。
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.
更改按钮返回到提交似乎获得通过,但我真的不希望一个可怕的按钮,而不是图像。
Changing the button back to submit seems to get passed it, but I don't really want a horrible button rather than the image.
推荐答案
好了,原来的&LT;输入类型=形象
返回图像的坐标点击时,未指定的值。
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;
}
这篇关于MVC4的OAuth造成空值异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!