Dot-Net

讓 ClaimsResponse 與 DotNetOpenId 一起工作的秘訣是什麼?

  • March 12, 2012

讓 ClaimsResponse 與DotNetOpenId一起工作的秘訣是什麼?

例如,在這段程式碼(來自Scott Hanselman 的部落格)中,ClaimsResponse 對象應該有很多不錯的小東西,例如“暱稱”和“電子郵件地址”,但 ClaimsResponse 對象本身是“null”:

OpenIdRelyingParty openid = new OpenIdRelyingParty();  
if (openid.Response != null)  
{  
   // Stage 3: OpenID Provider sending assertion response  
   switch (openid.Response.Status)  
   {  
      case AuthenticationStatus.Authenticated:  
         ClaimsResponse fetch = openid.Response.GetExtension(typeof(ClaimsResponse)) as ClaimsResponse;  
         string nick = fetch.Nickname;  
         string homepage = openid.Response.ClaimedIdentifier;  
         string email = fetch.Email;  
         string comment = Session["pendingComment"] as string;  
         string entryId = Session["pendingEntryId"] as string;  
         if (String.IsNullOrEmpty(comment) == false && String.IsNullOrEmpty(entryId) == false)  
         {  
            AddNewComment(nick, email, homepage, comment, entryId, true);  
         }  
         break;  
   }  
}  

起初,我認為這是因為我沒有使用“ClaimsRequest”重定向到提供者……但使用此程式碼重定向到 OpenId 提供者仍然無濟於事:

OpenIdRelyingParty openid = new OpenIdRelyingParty();  
IAuthenticationRequest req = openid.CreateRequest(openid_identifier.Text);  
ClaimsRequest fetch = new ClaimsRequest();  
fetch.Email = DemandLevel.Require;  
fetch.Nickname = DemandLevel.Require;  
req.AddExtension(fetch);  
req.RedirectToProvider(); 

我究竟做錯了什麼?或者其他開發者是否經歷過同樣的痛苦?

你的程式碼看起來不錯。但請注意,並非所有 OP 都支持您正在使用的 sreg 擴展。如果您正在驗證的 OP 不支持它,那麼響應擴展將為您所看到的 null。所以空檢查總是一個好主意。

myopenid.com 支持 sreg,如果您正在尋找要測試的 OP。

我不知道您是否解決了問題,但經過數小時的努力,我找到了解決方案。實際上,您需要更改您的 web.config 文件以聲明電子郵件,這裡的全名是 web.config,它適用於我。我從 nerddinner 項目下載了它。實際上,我複制了除 web.config 之外的所有內容,但沒有收到電子郵件欄位。所以後來我發現還有其他問題。我從 nerddinner 項目中複製了 web.config,一切正常。

這是文件,如果你不想去 nerddinner 項目。

<?xml version="1.0" encoding="utf-8"?>
<!-- 
   Note: As an alternative to hand editing this file you can use the 
   web admin tool to configure settings for your application. Use
   the Website->Asp.Net Configuration option in Visual Studio.
   A full list of settings and comments can be found in 
   machine.config.comments usually located in 
   \Windows\Microsoft.Net\Framework\v2.x\Config 
-->
<configuration>
 <configSections>
   <sectionGroup name="elmah">
    </sectionGroup>
   <section name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection" requirePermission="false" allowLocation="true" />
 </configSections>
 <connectionStrings configSource="connectionStrings.config">
 </connectionStrings>
 <dotNetOpenAuth>
   <openid>
     <relyingParty>
       <behaviors>
         <add type="DotNetOpenAuth.OpenId.Behaviors.AXFetchAsSregTransform, DotNetOpenAuth" />
       </behaviors>
     </relyingParty>
   </openid>
 </dotNetOpenAuth>
 <system.web>
   <!-- 
           Set compilation debug="true" to insert debugging 
           symbols into the compiled page. Because this 
           affects performance, set this value to true only 
           during development.
   -->
   <compilation debug="true" targetFramework="4.0">
     <assemblies>
       <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
       <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
       <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
       <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
       <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
     </assemblies>
   </compilation>
   <!--
           The <authentication> section enables configuration 
           of the security authentication mode used by 
           ASP.NET to identify an incoming user. 
   -->
   <authentication mode="Forms">
     <forms loginUrl="~/Account/Logon" />
   </authentication>
   <membership>
     <providers>
       <clear />
       <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />
     </providers>
   </membership>
   <profile>
     <providers>
       <clear />
       <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" />
     </providers>
   </profile>
   <roleManager enabled="false">
     <providers>
       <clear />
       <add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
       <add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
     </providers>
   </roleManager>
   <customErrors mode="RemoteOnly" defaultRedirect="/Dinners/Trouble">
     <error statusCode="404" redirect="/Dinners/Confused" />
   </customErrors>

   <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
     <namespaces>
       <add namespace="System.Web.Mvc" />
       <add namespace="System.Web.Mvc.Ajax" />
       <add namespace="System.Web.Mvc.Html" />
       <add namespace="System.Web.Routing" />
       <add namespace="System.Globalization" />
       <add namespace="System.Linq" />
       <add namespace="System.Collections.Generic" />
     </namespaces>
   </pages>
   <httpHandlers>
     &lt;add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /&gt;
   &lt;/httpHandlers&gt;
   <httpModules>
   &lt;/httpModules&gt;
   &lt;trace enabled="true" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" /&gt;
 &lt;/system.web&gt;
 &lt;!-- 
       The system.webServer section is required for running ASP.NET AJAX under Internet
       Information Services 7.0.  It is not necessary for previous version of IIS.
 --&gt;
 &lt;system.webServer&gt;
   &lt;validation validateIntegratedModeConfiguration="false" /&gt;
   &lt;modules runAllManagedModulesForAllRequests="true"&gt;
   &lt;/modules&gt;
   &lt;handlers&gt;
     &lt;remove name="MvcHttpHandler" /&gt;
     &lt;remove name="UrlRoutingHandler" /&gt;
     &lt;add name="MvcHttpHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /&gt;
   &lt;/handlers&gt;
 &lt;/system.webServer&gt;
 &lt;runtime&gt;
   &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
     &lt;dependentAssembly&gt;
       &lt;assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /&gt;
       &lt;bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" /&gt;
     &lt;/dependentAssembly&gt;
   &lt;/assemblyBinding&gt;
 &lt;/runtime&gt;
 &lt;appSettings&gt;
   &lt;add key="microsoft.visualstudio.teamsystems.backupinfo" value="8;web.config.backup" /&gt;
   &lt;!-- Fill in your various consumer keys and secrets here to make the sample work. --&gt;
   &lt;!-- You must get these values by signing up with each individual service provider. --&gt;
   &lt;!-- Twitter sign-up: https://twitter.com/oauth_clients --&gt;
   &lt;add key="twitterConsumerKey" value="" /&gt;
   &lt;add key="twitterConsumerSecret" value="" /&gt;
 &lt;/appSettings&gt;
 &lt;system.serviceModel&gt;
   &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" /&gt;
 &lt;/system.serviceModel&gt;
&lt;/configuration&gt; 

引用自:https://stackoverflow.com/questions/292993