假设您使用的是 DotNetOpenAuth,请查看Stack Exchange Data Explorer的身份验证代码。
本质上,您只需在请求中询问电子邮件地址:
request.AddExtension(
new ClaimsRequest
{
Email = DemandLevel.Require,
}
);
然后根据您的域白名单检查返回的地址(我假设您已经只接受谷歌 OpenID)
var sreg = response.GetExtension<ClaimsResponse>();
If (!HasWhiteListedDomain(sreg.Email)) {
// Fail Here
}
请注意,需要将这些代码添加到您的 Web.config 以获取用于获取上述电子邮件的确切代码:
<configSections>
<section name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection" requirePermission="false" allowLocation="true" />
</configSections>
<dotNetOpenAuth>
<openid>
<relyingParty>
<behaviors>
<!-- The following OPTIONAL behavior allows RPs to use SREG only, but be compatible
with OPs that use Attribute Exchange (in various formats). -->
<add type="DotNetOpenAuth.OpenId.Behaviors.AXFetchAsSregTransform, DotNetOpenAuth" />
</behaviors>
</relyingParty>
</openid>
</dotNetOpenAuth>
编辑:
如果使用OAuthWebSecurity
获取电子邮件将看起来像这样:
var userDataFromProvider = result.ExtraData;
var email = userDataFromProvider["email"];
资源