我正在尝试遵循 SocialBootstrapApi 示例并仅使用 4 个提供程序(Facebook、Twitter、GoogleOpenId 和 YahooOpenId)为我的 Web 应用程序设置身份验证。我还想将用户的元数据(FacebookUserName 等)存储在自定义模式中(用户表是预先存在的)。
需要注册服务吗?我宁愿只使用他们的 OAuth 对用户进行身份验证,然后立即将数据保存为新用户(而不是使用两步注册然后进行身份验证过程)。
我尝试了以下代码,但是当我 GET 或 POST 到 AuthService 的 URL 时,我收到一个错误:
Handler for Request not found:
Request.ApplicationPath: /
Request.CurrentExecutionFilePath: /api/auth/googleopenid
Request.FilePath: /api/auth/googleopenid
Request.HttpMethod: GET
...
应用主机.cs
public class AppHost : AppHostBase {
public AppHost() : base("...", typeof(HelloService).Assembly) { }
public override void Configure(Container container) {
container.Register<ICacheClient>(new MemoryCacheClient());
ConfigureAuth(container);
ConfigureRoutes();
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
ServiceStackController.CatchAllController = reqCtx => container.TryResolve<HomeController>();
}
void ConfigureAuth(Container container) {
var appSettings = new AppSettings();
Plugins.Add(new AuthFeature(
() => new CustomUserSession(),
new IAuthProvider[] {
new TwitterAuthProvider(appSettings),
new FacebookAuthProvider(appSettings),
new GoogleOpenIdOAuthProvider(appSettings),
new YahooOpenIdOAuthProvider(appSettings)
}));
}
void ConfigureRoutes() {
Routes
.Add<Hello>("/hello")
.Add<Hello>("/hello/{Name*}")
;
}
public static void Start() {
new AppHost().Init();
}
}
自定义用户会话.cs
public class CustomUserSession : AuthUserSession {
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo) {
base.OnAuthenticated(authService, session, tokens, authInfo);
var user = session.TranslateTo<User>();
user.ID = long.Parse(session.UserAuthId);
user.GravatarUrl64 = !session.Email.IsNullOrEmpty()
? CreateGravatarUrl(session.Email)
: null;
foreach (var authToken in session.ProviderOAuthAccess) {
if (authToken.Provider == FacebookAuthProvider.Name) {
user.FacebookName = authToken.DisplayName;
user.FacebookFirstName = authToken.FirstName;
user.FacebookLastName = authToken.LastName;
user.FacebookEmail = authToken.Email;
} else if (authToken.Provider == TwitterAuthProvider.Name) {
user.TwitterName = authToken.DisplayName;
} else if (authToken.Provider == GoogleOpenIdOAuthProvider.Name) {
user.GoogleUserId = authToken.UserId;
user.GoogleFullName = authToken.FullName;
user.GoogleEmail = authToken.Email;
} else if (authToken.Provider == YahooOpenIdOAuthProvider.Name) {
user.YahooUserId = authToken.UserId;
user.YahooFullName = authToken.FullName;
user.YahooEmail = authToken.Email;
}
}
authService.TryResolve<IDbConnectionFactory>().Run(db => db.Save(user));
}
网页配置
<appSettings>
<add key="oauth.facebook.Permissions" value="email,read_stream,offline_access" />
<add key="oauth.facebook.AppId" value="..." />
<add key="oauth.facebook.AppSecret" value="..." />
<add key="oauth.facebook.RedirectUrl" value="http://localhost:3000" />
<add key="oauth.facebook.CallbackUrl" value="http://localhost:3000/api/auth/facebook" />
<add key="oauth.twitter.ConsumerKey" value="..." />
<add key="oauth.twitter.ConsumerSecret" value="..." />
<add key="oauth.twitter.RedirectUrl" value="http://localhost:3000" />
<add key="oauth.twitter.CallbackUrl" value="http://localhost:3000/api/auth/twitter" />
<add key="oauth.GoogleOpenId.RedirectUrl" value="http://localhost:3000" />
<add key="oauth.GoogleOpenId.CallbackUrl" value="http://localhost:3000/api/auth/googleopenid" />
<add key="oauth.YahooOpenId.RedirectUrl" value="http://localhost:3000" />
<add key="oauth.YahooOpenId.CallbackUrl" value="http://localhost:3000/api/auth/yahooopenid" />
</appSettings>