C# windows应用程序中的身份验证;无法使用未指定网关URI的MobileServiceClient。”;

C# windows应用程序中的身份验证;无法使用未指定网关URI的MobileServiceClient。”;,c#,azure,oauth,win-universal-app,C#,Azure,Oauth,Win Universal App,我正在尝试在我的通用应用程序中实现用户身份验证(不使用移动服务),如本文所述。但是,我遇到以下错误: 无法使用未指定网关URI的MobileServiceClient 由于我没有使用移动服务进行身份验证,也没有在客户端通用应用程序中进行身份验证,所以我不确定这里的url是什么。当前,“”是我在中拥有的后端应用程序的url(浏览所有>MyApp>设置>url) 在我的共享App.xaml.cs中: //Problem may be here as per my understanding publ

我正在尝试在我的通用应用程序中实现用户身份验证(不使用移动服务),如本文所述。但是,我遇到以下错误:

无法使用未指定网关URI的MobileServiceClient

由于我没有使用移动服务进行身份验证,也没有在客户端通用应用程序中进行身份验证,所以我不确定这里的url是什么。当前,“”是我在中拥有的后端应用程序的url(浏览所有>MyApp>设置>url)

在我的共享App.xaml.cs中:

//Problem may be here as per my understanding
public static MobileServiceClient MobileService = 
    new MobileServiceClient("http://MyApp.azurewebsites.net");
private async System.Threading.Tasks.Task AuthenticateAsync()
{
    string message;

    // This sample uses the Microsoft provider.
    var provider = "MicrosoftAccount";    

    // Use the PasswordVault to securely store and access credentials.
    PasswordVault vault = new PasswordVault();
    PasswordCredential credential = null;    
    while (credential == null)
    {
        try
        {
            // Try to get an existing credential from the vault.
            credential = vault.FindAllByResource(provider).FirstOrDefault();
        }
        catch (Exception)
        {
            // When there is no matching resource an error occurs, 
            // which we ignore.
        }    
        if (credential != null)
        {
            // Create a user from the stored credentials.
            user = new MobileServiceUser(credential.UserName);
            credential.RetrievePassword();
            user.MobileServiceAuthenticationToken = credential.Password;

            // Set the user from the stored credentials.
            App.MobileService.CurrentUser = user;

            try
            {
                // Try to return an item now to determine 
                // if the cached credential has expired.
                await App.MobileService
                    .GetTable<TodoItem>().Take(1).ToListAsync();
            }
            catch (MobileServiceInvalidOperationException ex)
            {
                if (ex.Response.StatusCode == 
                        System.Net.HttpStatusCode.Unauthorized)
                {
                    // Remove the credential with the expired token.
                    vault.Remove(credential);
                    credential = null;
                    continue;
                }
            }
        }
        else
        {
            try
            {
                // Login with the identity provider.
                // Here I am getting the exception.
                user = await App.MobileService.LoginAsync(provider);

                // Create and store the user credentials.
                credential = new PasswordCredential(provider,
                user.UserId, user.MobileServiceAuthenticationToken);
                vault.Add(credential);
            }
            catch (MobileServiceInvalidOperationException ex)
            {
                message = "You must log in. Login Required";
            }
        }
        message = string.Format("You are now logged in - {0}", user.UserId);
        var dialog = new MessageDialog(message);
        dialog.Commands.Add(new UICommand("OK"));
        await dialog.ShowAsync();
    }
}
在我的共享主页中。cs:

//Problem may be here as per my understanding
public static MobileServiceClient MobileService = 
    new MobileServiceClient("http://MyApp.azurewebsites.net");
private async System.Threading.Tasks.Task AuthenticateAsync()
{
    string message;

    // This sample uses the Microsoft provider.
    var provider = "MicrosoftAccount";    

    // Use the PasswordVault to securely store and access credentials.
    PasswordVault vault = new PasswordVault();
    PasswordCredential credential = null;    
    while (credential == null)
    {
        try
        {
            // Try to get an existing credential from the vault.
            credential = vault.FindAllByResource(provider).FirstOrDefault();
        }
        catch (Exception)
        {
            // When there is no matching resource an error occurs, 
            // which we ignore.
        }    
        if (credential != null)
        {
            // Create a user from the stored credentials.
            user = new MobileServiceUser(credential.UserName);
            credential.RetrievePassword();
            user.MobileServiceAuthenticationToken = credential.Password;

            // Set the user from the stored credentials.
            App.MobileService.CurrentUser = user;

            try
            {
                // Try to return an item now to determine 
                // if the cached credential has expired.
                await App.MobileService
                    .GetTable<TodoItem>().Take(1).ToListAsync();
            }
            catch (MobileServiceInvalidOperationException ex)
            {
                if (ex.Response.StatusCode == 
                        System.Net.HttpStatusCode.Unauthorized)
                {
                    // Remove the credential with the expired token.
                    vault.Remove(credential);
                    credential = null;
                    continue;
                }
            }
        }
        else
        {
            try
            {
                // Login with the identity provider.
                // Here I am getting the exception.
                user = await App.MobileService.LoginAsync(provider);

                // Create and store the user credentials.
                credential = new PasswordCredential(provider,
                user.UserId, user.MobileServiceAuthenticationToken);
                vault.Add(credential);
            }
            catch (MobileServiceInvalidOperationException ex)
            {
                message = "You must log in. Login Required";
            }
        }
        message = string.Format("You are now logged in - {0}", user.UserId);
        var dialog = new MessageDialog(message);
        dialog.Commands.Add(new UICommand("OK"));
        await dialog.ShowAsync();
    }
}
private async System.Threading.Tasks.Task authenticatesync()
{
字符串消息;
//此示例使用Microsoft提供程序。
var provider=“MicrosoftAccount”;
//使用PasswordVault安全地存储和访问凭据。
PasswordVault=新的PasswordVault();
PasswordCredential-credential=null;
while(凭证==null)
{
尝试
{
//尝试从vault中获取现有凭据。
凭证=vault.FindAllByResource(提供程序).FirstOrDefault();
}
捕获(例外)
{
//如果没有匹配的资源,则会发生错误,
//我们忽视了这一点。
}    
如果(凭证!=null)
{
//从存储的凭据创建用户。
用户=新的MobileServiceUser(凭证.用户名);
credential.RetrievePassword();
user.MobileServiceAuthenticationToken=凭证.Password;
//从存储的凭据设置用户。
App.MobileService.CurrentUser=用户;
尝试
{
//现在尝试返回一个项目以确定
//如果缓存的凭据已过期。
等待App.MobileService
.GetTable().Take(1.toListSync();
}
捕获(MobileServiceInvalidOperationException ex)
{
如果(例如,Response.StatusCode==
System.Net.HttpStatusCode.Unauthorized)
{
//删除带有过期令牌的凭据。
删除(凭证);
凭证=null;
继续;
}
}
}
其他的
{
尝试
{
//使用身份提供者登录。
//我得到了一个例外。
user=wait App.MobileService.LoginAsync(提供者);
//创建并存储用户凭据。
凭证=新密码凭证(提供程序,
user.UserId、user.MobileServiceAuthenticationToken);
添加(凭证);
}
捕获(MobileServiceInvalidOperationException ex)
{
message=“您必须登录,需要登录”;
}
}
message=string.Format(“您现在登录到-{0}”,user.UserId);
var dialog=新建消息对话框(消息);
添加(新的UICommand(“OK”);
wait dialog.ShowAsync();
}
}

那么什么是网关Uri,我做错了什么,以及如何在没有Azure移动服务(托管在Azure上的MVC项目)的情况下仅在应用程序中实现身份验证。

应用程序需要有一个关联的网关。进一步阅读。您将需要这样的代码:

public static MobileServiceClient MobileService = new MobileServiceClient(
  mobileAppUri: "https://mvp2015mobileapp.azurewebsites.net", 
  gatewayUri: "https://mvp2015b14929cfc35019b24a7daaf694.azurewebsites.net",
  applicationKey: string.Empty);
查找现有的应用程序服务网关 从portal.azure.com打开您的移动应用程序,并单击其资源组链接。资源组刀片服务器打开后,单击其摘要面板。如果您已经有网关,它将在列表中

创建新的应用程序服务网关 从portal.azure.com打开您的移动应用程序,选择设置,然后选择移动用户身份验证。您将能够从那里创建一个新网关。下面是一个屏幕截图,显示了在何处创建网关(还显示了资源组链接)

检索网关URL 一旦找到网关,就可以获取其URL