gpt4 book ai didi

android - 身份验证 - Xamarin.Forms、Azure 移动应用

转载 作者:行者123 更新时间:2023-11-30 01:08:42 26 4
gpt4 key购买 nike

我有一个连接到 Azure 移动应用的身份验证服务的 Xamarin.Forms 应用(iOS、Android、UWP)。从我读到的内容来看,实现起来似乎非常简单。我正在 UWP 项目和 Android 中进行测试,两者都得到相同的结果。

登录时,我收到一条可爱的消息“现在无法连接到您需要的服务。”我的代码没有发现任何错误。这里可能发生了什么?

Windows UWP:

public async Task<bool> Authenticate()
{
string message = string.Empty;
var success = false;

try
{
// Sign in with Facebook login using a server-managed flow.
if (user == null)
{
user = await TaskService.DefaultService.CurrentClient.LoginAsync(MobileServiceAuthenticationProvider.Facebook);
if (user != null)
{
success = true;
message = string.Format("You are now signed-in as {0}.", user.UserId);
}
}

}
catch (Exception ex)
{
message = string.Format("Authentication Failed: {0}", ex.Message);
}

// Display the success or failure message.
await new MessageDialog(message, "Sign-in result").ShowAsync();

return success;
}

安卓:

public async Task<bool> Authenticate()
{
var success = false;
var message = string.Empty;
try
{
// Sign in with Facebook login using a server-managed flow.
user = await TaskService.DefaultService.CurrentClient.LoginAsync(this,
MobileServiceAuthenticationProvider.Facebook);
if (user != null)
{
message = string.Format("you are now signed-in as {0}.",
user.UserId);
success = true;
}
}
catch (Exception ex)
{
message = ex.Message;
}

// Display the success or failure message.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetMessage(message);
builder.SetTitle("Sign-in result");
builder.Create().Show();

return success;
}

无法连接到服务错误: Can't connect to service error in Windows UWP

应用程序在 Facebook 上的设置: Facebook App Settings

访问过:

编辑:我试过将客户端代码移动到 App.cs:

private static MobileServiceClient _Client;
public static MobileServiceClient Client
{
get
{
if(_Client == null) _Client = new MobileServiceClient(AppConstants.AzureMobileServiceURL);
return _Client;
}
set { _Client = value; }
}

MainPage.xaml.cs 中的新调用:

uesr = await Slated.App.Client.LoginAsync(MobileServiceAuthenticationProvider.Facebook);

同样的结果!

另一条注意事项:我的 Azure 移动服务 URL 确实包括 https://

编辑 2:下面用于登录后端的代码,在重定向到/login/facebook 时看起来是一个问题,其余的身份验证似乎可以继续进行。仅供引用 - 将敏感数据替换为 ____

2016-07-29T18:38:43  PID[6684] Verbose     Received request: GET https://________.azurewebsites.net/login/facebook
2016-07-29T18:38:43 PID[6684] Information Redirecting: https://www.facebook.com/dialog/oauth?response_type=code&client_id=_____________&redirect_uri=https%3A%2F%2F________.azurewebsites.net%2F.auth%2Flogin%2Ffacebook%2Fcallback&scope=public_profile&state=_____________________________&display=popup
2016-07-29T18:38:50 PID[6684] Verbose Received request: GET https://________.azurewebsites.net/.auth/login/facebook/callback?code=____________________
2016-07-29T18:38:50 PID[6684] Verbose Calling into external HTTP endpoint GET https://graph.facebook.com/oauth/access_token.
2016-07-29T18:38:51 PID[6684] Verbose Calling into external HTTP endpoint GET https://graph.facebook.com/oauth/access_token.
2016-07-29T18:38:51 PID[6684] Verbose Calling into external HTTP endpoint GET https://graph.facebook.com/me.
2016-07-29T18:38:51 PID[6684] Information Login completed for 'Thomas Gardner'. Provider: 'facebook'.
2016-07-29T18:38:51 PID[6684] Verbose Writing 'AppServiceAuthSession' cookie for site '________.azurewebsites.net'. Length: 512.
2016-07-29T18:38:51 PID[6684] Information Redirecting: https://________.azurewebsites.net/login/facebook
2016-07-29T18:38:51 PID[6684] Verbose Received request: GET https://________.azurewebsites.net/login/facebook
2016-07-29T18:38:51 PID[6684] Verbose Found 'AppServiceAuthSession' cookie for site '________.azurewebsites.net'. Length: 512.
2016-07-29T18:38:51 PID[6684] Verbose Authenticated Thomas Gardner successfully using 'Session Cookie' authentication.
2016-07-29T18:38:52 PID[6684] Verbose Received request: GET https://________.azurewebsites.net/login/facebook
2016-07-29T18:38:52 PID[6684] Verbose Found 'AppServiceAuthSession' cookie for site '________.azurewebsites.net'. Length: 512.
2016-07-29T18:38:52 PID[6684] Verbose Authenticated __________ successfully using 'Session Cookie' authentication.
2016-07-29T18:38:52 PID[6684] Information Request, Method=GET, Url=https://________.azurewebsites.net/login/facebook, Message='https://________.azurewebsites.net/login/facebook'
2016-07-29T18:38:52 PID[6684] Information Message='Will use same 'JsonMediaTypeFormatter' formatter', Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance
2016-07-29T18:38:52 PID[6684] Information Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate
2016-07-29T18:38:52 PID[6684] Information Response, Status=404 (NotFound), Method=GET, Url=https://________.azurewebsites.net/login/facebook, Message='Content-type='application/json; charset=utf-8', content-length=unknown'

Backend Code: 复制自微软的模板代码。这家初创公司没有做太多事情。Startup.Mobile.App.cs

public partial class Startup
{
public static void ConfigureMobileApp(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();

//For more information on Web API tracing, see http://go.microsoft.com/fwlink/?LinkId=620686
config.EnableSystemDiagnosticsTracing();

new MobileAppConfiguration()
.UseDefaultConfiguration()
.ApplyTo(config);

// Use Entity Framework Code First to create database tables based on your DbContext
Database.SetInitializer(new SlatedInitializer());

// To prevent Entity Framework from modifying your database schema, use a null database initializer
// Database.SetInitializer<SlatedContext>(null);

MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();

if (string.IsNullOrEmpty(settings.HostName))
{
// This middleware is intended to be used locally for debugging. By default, HostName will
// only have a value when running in an App Service application.
app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
{
SigningKey = ConfigurationManager.AppSettings["SigningKey"],
ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] },
ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] },
TokenHandler = config.GetAppServiceTokenHandler()
});
}
app.UseWebApi(config);
}
}

public class SlatedInitializer : CreateDatabaseIfNotExists<SlatedContext>
{
protected override void Seed(SlatedContext context)
{
/*List<Tasks> todoItems = new List<Tasks>
{
new Tasks { Id = Guid.NewGuid().ToString(), Text = "First item", Complete = false },
new Tasks { Id = Guid.NewGuid().ToString(), Text = "Second item", Complete = false },
};

foreach (Tasks todoItem in todoItems)
{
context.Set<Tasks>().Add(todoItem);
}*/

base.Seed(context);
}
}

最佳答案

从正在使用的 URI 来看,您似乎在混合使用移动服务和移动应用程序的包。两者兼容。

要了解更多信息,请参阅 Client and server versioning in Mobile Apps and Mobile Services .

在服务器上,您应该使用 Microsoft.Azure.Mobile.Server.*。确保您没有 WindowsAzure.MobileServices.Backend 形式的包。

在客户端,您必须使用包 Microsoft.Azure.Mobile.Client .

关于android - 身份验证 - Xamarin.Forms、Azure 移动应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38649080/

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