我有一个应用程序,允许用户通过Facebook登录,一旦用户输入其凭据 - 我的API请求将用户保存到数据库中并自动生成用户令牌(这是唯一的给每个用户)。为了显示用户特定详细信息,一旦用户登录 - 需要引用令牌。我正在尝试将这个令牌转到PCL项目中,但它仅用于代币。当我尝试传递另一个字符串之类的名称时,它会传递正确的值。任何帮助将不胜感激。谢谢
droid中的Facebookrender:
public class FacebookRender : PageRenderer
{
public FacebookRender()
{
CustomerService customerService = new CustomerService();
String error;
var activity = this.Context as Activity;
var auth = new OAuth2Authenticator(
clientId: "",
scope: "",
authorizeUrl: new Uri("https://www.facebook.com/dialog/oauth/"),
redirectUrl: new Uri("https://www.facebook.com/connect/login_success.html")
);
auth.Completed += async (sender, eventArgs) =>
{
try
{
if (eventArgs.IsAuthenticated)
{
await AccountStore.Create().SaveAsync(eventArgs.Account, "FacebookProviderKey");
var accessToken = eventArgs.Account.Properties["access_token"].ToString();
var expiresIn = Convert.ToDouble(eventArgs.Account.Properties["expires_in"]);
var expiryDate = DateTime.Now + TimeSpan.FromSeconds(expiresIn);
var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me?fields=email,first_name,last_name,gender,picture"), null, eventArgs.Account);
var response = await request.GetResponseAsync();
var obj = JObject.Parse(response.GetResponseText());
var id = obj["id"].ToString().Replace(""", "");
var name = obj["first_name"].ToString().Replace(""", "");
var surname = obj["last_name"].ToString().Replace(""", "");
var gender = obj["gender"].ToString().Replace(""", "");
//var email = obj["email"].ToString().Replace(""", "");
Customer.Customers cust = new Customer.Customers();
cust.Credentials = new Customer.Credentials();
cust.Name = name;
cust.Surname = surname;
cust.Email = "";
cust.MobilePhone = "";
cust.DOB = DateTime.Now;
cust.Number = "";
cust.City = "";
cust.Region = "";
cust.Country = "";
cust.DeviceToken = "sample";
cust.Credentials.SecretKey = "";
await customerService.AddCustomer(cust);
App.SaveToken(cust.Credentials.Token); - **//This is where I am passing the token**
App.NavigateToProfile(string.Format(name + surname));
}
else
{
App.NavigateToProfile("Invalid Login");
}
}
catch(Exception ex)
{
error = ex.Message;
}
};
activity.StartActivity(auth.GetUI(activity));
}
app.cs
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
public static void NavigateToProfile(string message)
{
App.Current.MainPage = (new Profile(message));
}
static string _Token;
public static string Token
{
get { return _Token; }
}
public static void SaveToken(string token)
{
_Token = token;
}
aboutpage.cs-我正在标签中传递令牌,只是为了查看它是否通过
public partial class About : ContentPage
{
private Label _lbltoken;
public About()
{
//InitializeComponent();
Appearing += (object s, EventArgs a) => {
_lbltoken.Text = App.Token;
};
string tk = App.Token;
_lbltoken = new Label()
{
FontSize = 20,
HorizontalOptions = LayoutOptions.CenterAndExpand,
Text = tk,
};
var stack = new StackLayout
{
VerticalOptions = LayoutOptions.StartAndExpand,
Children = { _lbltoken },
};
Content = stack;
}
}
您可以使用MesdagingCenter。结果可能会作为结果发送,例如单击按钮,系统事件或其他一些事件。订户可能正在聆听以更改用户界面的外观,保存数据或触发其他操作。
更多信息
我现在并不是现在,如果它的好主意在应用程序类中使用静态字段。xamarin访问所有带有服务定位器的字段,App.Current.[property]
我建议您尝试将这些字段更改为public
string _Token;
public string Token
{
get { return _Token; }
}
public void SaveToken(string token)
{
_Token = token;
}
并将其与App.Current.SaveToken(token)
或App.Current.Token