ServiceStack and Auth0

瘦欲@ 提交于 2019-12-24 12:14:51

问题


I am looking to use Auth0 as the authentication provider for ServiceStack. There is a great sample application documented at Auth0 which applies & works well when working with ServiceStack and using ServiceStack.Host.MVC: https://auth0.com/docs/quickstart/webapp/servicestack/01-login.

However, I am at a loss how to construct the authorization URL and redirect the user to that URL in a scenario where I am NOT using MVC & the AccountController to redirect the user. How can I construct the redirect URLs using ServiceStack Auth Plugin, if I want to replicate the logic as per MVC sample code below:

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());
 }
}

回答1:


For all who are interested,here is the solution I ended up adopting.

Steps:

1) Create an Auth0 plugin (see gist here)

2) Register the Plugin in your AppHost.

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

3) Add the relevant keys in your 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>


来源:https://stackoverflow.com/questions/49494449/servicestack-and-auth0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!