ServiceStack和Auth0 [英] ServiceStack and Auth0
问题描述
我希望使用Auth0作为ServiceStack的身份验证提供程序. Auth0记录了一个很棒的示例应用程序,该应用程序适用于&与ServiceStack一起使用并使用ServiceStack.Host.MVC时,效果很好: https://auth0.com/docs/quickstart/webapp/servicestack/01-login .
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.
但是,在我不使用MVC& amp;的情况下,如何构造授权URL并将用户重定向到该URL,我一头雾水. AccountController来重定向用户.如果要根据以下MVC示例代码复制逻辑,如何使用ServiceStack Auth插件构造重定向URL:
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());
}
}
推荐答案
对于所有感兴趣的人,这是我最终采用的解决方案.
For all who are interested,here is the solution I ended up adopting.
步骤:
1)创建一个Auth0插件(请参见要点此处)
1) Create an Auth0 plugin (see gist here)
2)在您的AppHost中注册插件.
2) Register the Plugin in your AppHost.
Plugins.Add(new AuthFeature(() => new Auth0UserSession(), new IAuthProvider[] {
new Auth0Provider(appSettings,appSettings.GetString("oauth.auth0.OAuthServerUrl"))
}));
3)在Web.Config中添加相关密钥.
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>
这篇关于ServiceStack和Auth0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!