1

我希望使用 Auth0 作为 ServiceStack 的身份验证提供程序。在 Auth0 中记录了一个很棒的示例应用程序,它在使用 ServiceStack 和使用 ServiceStack.Host.MVC 时适用并且运行良好:https ://auth0.com/docs/quickstart/webapp/servicestack/01-login 。

但是,在我不使用 MVC 和 AccountController 重定向用户的情况下,我不知道如何构造授权 URL 并将用户重定向到该 URL。如果我想按照下面的 MVC 示例代码复制逻辑,如何使用 ServiceStack Auth Plugin 构造重定向 URL:

public class AccountController : Controller
{
  public ActionResult Login()
  {
    string clientId = WebConfigurationManager.AppSettings["oauth.auth0.AppId"];
    string domain = WebConfigurationManager.AppSettings["oauth.auth0.OAuthServerUrl"].Substring(8);

    var redirectUri = new UriBuilder(this.Request.Url.Scheme, this.Request.Url.Host, this.Request.Url.IsDefaultPort ? -1 : this.Request.Url.Port, "api/auth/auth0");

    var client = new AuthenticationApiClient(new Uri($"https://{domain}"));
    var authorizeUrlBuilder = client.BuildAuthorizationUrl()
        .WithClient(clientId)
        .WithRedirectUrl(redirectUri.ToString())
        .WithResponseType(AuthorizationResponseType.Code)
        .WithScope("openid profile")
        .WithAudience($"https://{domain}/userinfo");


    return Redirect(authorizeUrlBuilder.Build().ToString());
 }
}
4

1 回答 1

1

对于所有感兴趣的人,这是我最终采用的解决方案。

脚步:

1) 创建一个 Auth0 插件(请参阅此处的要点)

2) 在您的 AppHost 中注册插件。

Plugins.Add(new AuthFeature(() => new Auth0UserSession(), new IAuthProvider[] {
    new Auth0Provider(appSettings,appSettings.GetString("oauth.auth0.OAuthServerUrl"))
}));

3) 在您的 Web.Config 中添加相关键。

<appSettings>
   <add key="oauth.auth0.OAuthServerUrl" value="https://xxxxxxx.auth0.com" />
   <add key="oauth.auth0.AppId" value="xxxxxx" />
   <add key="oauth.auth0.AppSecret" value="xxxxxxx" />
</appSettings>
于 2018-04-19T04:35:23.513 回答