OpenAuth .Net Claims Request is always null
I've been using DoNetOpenAuth library and followe开发者_StackOverflowd the example here
The authentication is working, but even though I require an email, the claimsresponse is null. In fact, it doesn't matter what I require, claimsresponse is always null. Not sure what I'm doing wrong and I'd appreciate your help.
Thanks in advance.
Here is my login button code
protected void btnSubmit_Click( object sender, EventArgs e )
{
//Login button has been pushed. Add an extension and redirect
using (OpenIdRelyingParty openId = new OpenIdRelyingParty())
{
IAuthenticationRequest request = openId.CreateRequest( txtOpenID.Text );
request.AddExtension( new ClaimsRequest
{
Email = DemandLevel.Require,
Country = DemandLevel.Request,
TimeZone = DemandLevel.Request
} );
request.RedirectToProvider();
}
}
Here is the page load code. ClaimsResponse variable is always null though.
protected void Page_Load( object sender, EventArgs e )
{
OpenIdRelyingParty openId = new OpenIdRelyingParty();
var response = openId.GetResponse();
//check if we're processing a request
if(response != null)
{
switch ( response.Status )
{
case AuthenticationStatus.Authenticated:
//authentication worked. grab our required fields
var claimsResponse = response.GetExtension<ClaimsResponse>();
//TODO enter required fields into the database
break;
case AuthenticationStatus.Canceled:
//TODO handle cancel
break;
case AuthenticationStatus.Failed:
//TODO handle failed
break;
}
}
}
At last: The issue is in the web.config
add
<configuration> <configSections> <section name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection" requirePermission="false" allowLocation="true"/> </configSections> </configuration> <dotNetOpenAuth> <openid> <relyingParty> <behaviors> <add type="DotNetOpenAuth.OpenId.Behaviors.AXFetchAsSregTransform, DotNetOpenAuth"/> </behaviors> </relyingParty> </openid> </dotNetOpenAuth>
That is alll and it should work.
I face the same problem with both Yahoo & google. however this MVC sample works great in this sample working Sample.... I faced this problem when I tried to convert this solution to ordinary ASP.NET 2
protected void Page_Load(object sender, EventArgs e)
{
if (this.Request.HttpMethod == "POST")
{
var openid = new OpenIdRelyingParty();
string openid_identifier = this.openid_identifier.Text;
IAuthenticationRequest request = Openid.CreateRequest(Identifier.Parse(openid_identifier));
var fields = new ClaimsRequest();
fields.Email = DemandLevel.Require;
fields.FullName = DemandLevel.Require;
request.AddExtension(fields);
this.Response.ContentType = "text/html";
request.RedirectingResponse.Send();
return;
}
else
{
OpenIdRelyingParty openid = new OpenIdRelyingParty();
IAuthenticationResponse response = openid.GetResponse();
if (Request.Params["ReturnUrl"] != null)
Session["ReturnUrl"] = Request.Params["ReturnUrl"];
if (response != null && response.Status == AuthenticationStatus.Authenticated)
{
var claimUntrusted = response.GetUntrustedExtension<ClaimsResponse>();
var claim = response.GetExtension<ClaimsResponse>();
UserData userData = null;
if (claim != null)
{
userData = new UserData();
userData.Email = claim.Email;
userData.FullName = claim.FullName;
}
//now store Forms Authorisation cookie
IssueAuthTicket(userData, true);
//store ClaimedIdentifier it in Session
//(this would more than likely be something you would store in a database I guess
Session["ClaimedIdentifierMessage"] = response.ClaimedIdentifier;
//If we have a ReturnUrl we MUST be using forms authentication,
//so redirect to the original ReturnUrl
if (Session["ReturnUrl"] != null)
{
string url = Session["ReturnUrl"].ToString();
this.Response.Redirect(url,true);
return;
}
//This should not happen if all controllers have [Authorise] used on them
else
throw new InvalidOperationException("There is no ReturnUrl");
}
}
}
Scott Hanselman has a recent podcast on DoNetOpenAuth. On the podcast, Scott Arnott, author of DotNetOpenAuth, claims that Google's OAuth implementation doesn't properly conform to the specs and says it should fail rather than return null.
Turns out that Google does not send back any information you request. They will authenticate but that's about it. Hope this helps anyone else with this problem.
精彩评论