1

我对 ServiceStack 很陌生,所以如果我提出任何明显的问题,请原谅我的无知。

我有一个网站已经使用 dotnetopenauth 使用在线提供的普通示例对用户进行身份验证。有一个登录按钮,该按钮发布到此方法:

Public Sub ExecuteGoogleLogin()
    Dim Url As String = "https://www.google.com/accounts/o8/id"
    Dim OpenID As New OpenIdRelyingParty
    Dim HostedMeta As New HostMetaDiscoveryService() With {.UseGoogleHostedHostMeta = True}
    Dim ReturnUrl As String = Request.Url.ToString

    OpenID.DiscoveryServices.Insert(0, HostedMeta)

    Dim builder As New UriBuilder(ReturnUrl)
    Dim fetch As New FetchRequest()


    Dim Req = OpenID.CreateRequest(Url, Realm.AutoDetect, builder.Uri)

    fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email)
    fetch.Attributes.AddRequired(WellKnownAttributes.Name.First)
    fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last)

    Req.AddExtension(fetch)

    Req.RedirectToProvider()
End Sub

然后它将重定向回将检查响应的页面,例如

  Private Sub CheckOpenIDResponse()
    Dim Rp As New OpenIdRelyingParty
    Dim Resp = Rp.GetResponse()
    Dim Subsc As Subscriber

Select Case Resp.Status
            Case AuthenticationStatus.Authenticated
                Dim Fetch = Resp.GetExtension(Of FetchResponse)()

                Email = Fetch.GetAttributeValue(WellKnownAttributes.Contact.Email)
....

相当标准并且工作正常(目前仅支持谷歌)但它可以工作。我的 AppHost 工作正常,一些测试 Dto 按预期工作,现在只需要实现身份验证。所以我的问题是:

  1. 如何使用 servicestack 的身份验证类(GoogleOpenIdOAuthProvider 等)将此准系统代码转换为工作?并彻底摆脱 dotnetopenauth。或者...
  2. 在 ServiceStack 中,想办法使用 dotnetopenauth(或上面的代码)来实现身份验证。

也许我在 SS 文档中遗漏了一些明显的东西,但对于我的生活,我似乎无法弄清楚如何将它们放在一起。

为了从调用页面获取会话,我将我的用户对象(称为订阅者)包装在 CustomUserSession 中。

    Dim Ahost = ServiceStack.WebHost.Endpoints.EndpointHost.AppHost
    Dim Key = ServiceStack.ServiceInterface.SessionFeature.GetSessionKey()
    Dim Sess As CustomUserSession = Ahost.TryResolve(Of ServiceStack.CacheAccess.ICacheClient)().[Get](Of CustomUserSession)(Key)

然后从这里我按照我的意愿使用会话。

4

1 回答 1

3

不确定上面的代码如何与 ServiceStack 集成。有点偏见,但我会让 ServiceStack 使用GoogleOpenIdOAuthProvider. 此外,SocialBootstrapApi项目应该是一个很好的参考。

'{servicestack path}/auth/googleopenid下面的设置/配置为您提供了处理身份验证的 url 。

假设您安装了 ServiceStack ...

Nuget 安装(或只是参考 ServiceStack.Authention.OpenId.dll

在 AppHost 中添加带有 GoogleOpenIdOAuthProvider 的 AuthFeature 插件

public override void Configure(Funq.Container container)
{
    Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { new GoogleOpenIdOAuthProvider(new AppSettings())}));     
}

为 GoogleOpenIdOAuthProvider 添加一些特定的 url

<appSettings>
<add key="oauth.GoogleOpenId.RedirectUrl" value="http://localhost" />
<add key="oauth.GoogleOpenId.CallbackUrl" value="http://localhost/api/auth/GoogleOpenId" /> 
</appSettings>

Web.config 中的大量配置。应与 ServiceStack.Authentication.OpenId 的 NuGet 安装一起添加

<configsections>
    <sectionGroup name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection, DotNetOpenAuth">
      <section name="openid" type="DotNetOpenAuth.Configuration.OpenIdElement, DotNetOpenAuth" requirePermission="false" allowLocation="true" />
      <section name="oauth" type="DotNetOpenAuth.Configuration.OAuthElement, DotNetOpenAuth" requirePermission="false" allowLocation="true" />
      <section name="messaging" type="DotNetOpenAuth.Configuration.MessagingElement, DotNetOpenAuth" requirePermission="false" allowLocation="true" />
      <section name="reporting" type="DotNetOpenAuth.Configuration.ReportingElement, DotNetOpenAuth" requirePermission="false" allowLocation="true" />
    </sectionGroup>
  </configSections>


  <dotNetOpenAuth>
    <!-- This is an optional configuration section where aspects of dotnetopenauth can be customized. -->
    <!-- For a complete set of configuration options see http://www.dotnetopenauth.net/developers/code-snippets/configuration-options/ -->
    <openid>
      <relyingParty>
        <security requireSsl="false">
          <!-- Uncomment the trustedProviders tag if your relying party should only accept positive assertions from a closed set of OpenID Providers. -->
          <!--<trustedProviders rejectAssertionsFromUntrustedProviders="true">
                        <add endpoint="https://www.google.com/accounts/o8/ud" />
                    </trustedProviders>-->
        </security>
        <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.RelyingParty.Behaviors.AXFetchAsSregTransform, DotNetOpenAuth" />
        </behaviors>
      </relyingParty>
    </openid>
    <messaging>
      <untrustedWebRequest>
        <whitelistHosts>
          <!-- Uncomment to enable communication with localhost (should generally not activate in production!) -->
          <!--<add name="localhost" />-->
        </whitelistHosts>
      </untrustedWebRequest>
    </messaging>
    <!-- Allow DotNetOpenAuth to publish usage statistics to library authors to improve the library. -->
    <reporting enabled="true" />
  </dotNetOpenAuth>

访问 AuthUserSession 数据

在您的 ServiceStack 服务(继承自 Service 的类)中,您可以使用:

var sess = this.GetSession();

在 ServiceStack 之外,您可以执行以下操作:

var key = SessionFeature.GetSessionKey();
var sess = appHost.TryResolve<ICacheClient>().Get<AuthUserSession>(key);
于 2013-04-19T20:42:24.097 回答