gpt4 book ai didi

ServiceStack 和 FacebookAuthProvider

转载 作者:行者123 更新时间:2023-12-04 20:47:02 27 4
gpt4 key购买 nike

我一直在与 ServiceStack 合作,它是 Auth 提供者。特别是“FacebookAuthProvider”。
我的问题是该服务是从 iOS 应用程序调用的。这个应用程序已经有一个有效的访问 token ,我只想将此值传递给 servicestack facebook 身份验证。

我已经在 servicestack github 页面上看到了测试,但对我来说仍然没有意义。

是否可以将此访问 token 传递给服务堆栈,因此身份验证会跳过我请求许可的部分,因为我们已经在应用程序上完成了?

或者我以错误的方式接近这个?

最佳答案

我没有使用内置的 Facebook 身份验证提供程序,而是创建了自己的 CustomFacebookAuthProvider。
原因是内置版本需要浏览器将用户重定向到 facebook 进行身份验证,而我不需要那个。我已经有一个访问 token 。

所以基于正式版FacebookAuthProvider.cs我创造了我自己的。

using System;
using System.Collections.Generic;
using System.Net;
using Elmah;
using Mondohunter.Backend.BusinessLogic.Interfaces;
using ServiceStack.Common.Extensions;
using ServiceStack.Common.Web;
using ServiceStack.Configuration;
using ServiceStack.ServiceInterface;
using ServiceStack.ServiceInterface.Auth;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;

namespace Mondohunter.Interfaces
{
public class CustomFacebookAuthProvider : OAuthProvider
{
public const string Name = "facebook";
public static string Realm = "https://graph.facebook.com/";
public static string PreAuthUrl = "https://www.facebook.com/dialog/oauth";

public string AppId { get; set; }
public string AppSecret { get; set; }
public string[] Permissions { get; set; }

public CustomFacebookAuthProvider(IResourceManager appSettings)
: base(appSettings, Realm, Name, "AppId", "AppSecret")
{
this.AppId = appSettings.GetString("oauth.facebook.AppId");
this.AppSecret = appSettings.GetString("oauth.facebook.AppSecret");
}

public override object Authenticate(IServiceBase authService, IAuthSession session, Auth request)
{
var tokens = Init(authService, ref session, request);

try
{
if (request.oauth_token.IsNullOrEmpty())
throw new Exception();

tokens.AccessToken = request.oauth_token;
session.IsAuthenticated = true;

var json = AuthHttpGateway.DownloadFacebookUserInfo(request.oauth_token);
var authInfo = JsonSerializer.DeserializeFromString<Dictionary<string, string>>(json);

//Here i need to update/set userauth id to the email
//UpdateUserAuthId(session, authInfo["email"]);

authService.SaveSession(session, SessionExpiry);
OnAuthenticated(authService, session, tokens, authInfo);

//return json/xml/... response;
}
catch (WebException ex)
{
//return json/xml/... response;
}
catch (Exception ex)
{
//return json/xml/... response;
}
}

protected override void LoadUserAuthInfo(AuthUserSession userSession, IOAuthTokens tokens, Dictionary<string, string> authInfo)
{
if (authInfo.ContainsKey("id"))
tokens.UserId = authInfo.GetValueOrDefault("id");
if (authInfo.ContainsKey("name"))
tokens.DisplayName = authInfo.GetValueOrDefault("name");
if (authInfo.ContainsKey("first_name"))
tokens.FirstName = authInfo.GetValueOrDefault("first_name");
if (authInfo.ContainsKey("last_name"))
tokens.LastName = authInfo.GetValueOrDefault("last_name");
if (authInfo.ContainsKey("email"))
tokens.Email = authInfo.GetValueOrDefault("email");
if (authInfo.ContainsKey("gender"))
tokens.Gender = authInfo.GetValueOrDefault("gender");
if (authInfo.ContainsKey("timezone"))
tokens.TimeZone = authInfo.GetValueOrDefault("timezone");

LoadUserOAuthProvider(userSession, tokens);
}

public override void LoadUserOAuthProvider(IAuthSession authSession, IOAuthTokens tokens)
{
var userSession = authSession as CustomUserSession;
if (userSession == null) return;

userSession.Email = tokens.Email ?? userSession.PrimaryEmail ?? userSession.Email;
}
}
}

我希望这是有道理的。

关于ServiceStack 和 FacebookAuthProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15959069/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com