繁体   English   中英

如何保持用户登录Xamarin

[英]How to keep user logged in xamarin

您好,我正在尝试将用户登录状态保存在xamarin应用程序中。

官方教程只讨论如何登录,而不是如何保持用户登录。Microsoft教程 xamarin教程

我的问题是,每次重新启动应用程序时都必须运行Login方法,这会在关闭前显示空白屏幕。

登录方式

async void Login()
        {
            if(App.Authenticator != null && authenticated == false)
            {
                authenticated = await App.Authenticator.Authenticate();
            }
        }

认证方式

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

            try
            {

                    //Sign in with Active Directory Service
                user = await ShopItemManager.DefaultManager.CurrentClient.LoginAsync(this,
                    MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory);



                if (user != null)
                {
                    message = string.Format("You are now signed in as {0}", user.UserId);
                    success = true;
                    //store token
                }
            }
            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;
        }

对Xamrin不太了解,但是在本机android中,我们使用SharedPreferences保存登录状态。 在Android中使用SharedPreferences持久存储一些数据(即,在应用程序关闭后,该数据将持续存在)。 我们可以放置一个布尔值来检测用户是否在下一次启动时成功登录。

SharedPreferences等效的Xamarin.Android是一个称为ISharedPreferences的接口。

以相同的方式使用它,您将能够轻松地移植Android代码。


例如,要使用某些Context保存bool请执行以下操作:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (mContext);
ISharedPreferencesEditor editor = prefs.Edit ();
editor.PutBoolean ("key_login_successfully", true);
// editor.Commit();    // applies changes synchronously on older APIs
editor.Apply();        // applies changes asynchronously on newer APIs

使用以下命令访问保存的值:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (mContext);
mBool = prefs.GetBoolean ("key_login_successfully", <default value>);

从此示例中,您可以看到,一旦知道要使用的正确C#接口,其余的工作就很容易了。 关于如何在更复杂的情况下使用SharedPreferences ,有很多Android Java示例,可以使用ISharedPreferences轻松移植这些ISharedPreferences

有关更多信息,请阅读以下线程:

因此, 本教程包含其中的信息。 这有点涉及。

此代码存储密钥。 确实有另一个用户提到过。 虽然这是跨平台的。 公共静态类设置{私人静态ISettings AppSettings => CrossSettings.Current;

    const string UserIdKey = "userid";
    static readonly string UserIdDefault = string.Empty;

    const string AuthTokenKey = "authtoken";
    static readonly string AuthTokenDefault = string.Empty;

    public static string AuthToken
    {
        get { return AppSettings.GetValueOrDefault<string>(AuthTokenKey, AuthTokenDefault); }
        set  { AppSettings.AddOrUpdateValue<string>(AuthTokenKey, value); }
    }

    public static string UserId
    {
        get { return AppSettings.GetValueOrDefault<string>(UserIdKey, UserIdDefault);  }
        set { AppSettings.AddOrUpdateValue<string>(UserIdKey, value); }
    }

    public static bool IsLoggedIn => !string.IsNullOrWhiteSpace(UserId); 
}

但是,与客户端构造函数一起传递时,此Auth处理程序将确保如果保存了任何凭据,则将使用存储的凭据。

    class AuthHandler : DelegatingHandler
{
    public IMobileServiceClient Client { get; set; }

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        if (this.Client == null)
        {
            throw new InvalidOperationException("Make sure to set the 'Client' property in this handler before using it.");
        }

        // Cloning the request, in case we need to send it again
        var clonedRequest = await CloneRequest(request);
        var response = await base.SendAsync(clonedRequest, cancellationToken);

        if (response.StatusCode == HttpStatusCode.Unauthorized)
        {
            // Oh no! user is not logged in - we got a 401
            // Log them in, this time hardcoded with MicrosoftAccount but you would
            // trigger the login presentation in your application
            try
            {
                var user = await this.Client.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount, null);
                // we're now logged in again.

                // Clone the request
                clonedRequest = await CloneRequest(request);


                Settings.UserId = user.UserId;
                Settings.AuthToken = user.MobileServiceAuthenticationToken;

                clonedRequest.Headers.Remove("X-ZUMO-AUTH");
                // Set the authentication header
                clonedRequest.Headers.Add("X-ZUMO-AUTH", user.MobileServiceAuthenticationToken);

                // Resend the request
                response = await base.SendAsync(clonedRequest, cancellationToken);
            }
            catch (InvalidOperationException)
            {
                // user cancelled auth, so let’s return the original response
                return response;
            }
        }

        return response;
    }

    private async Task<HttpRequestMessage> CloneRequest(HttpRequestMessage request)
    {
        var result = new HttpRequestMessage(request.Method, request.RequestUri);
        foreach (var header in request.Headers)
        {
            result.Headers.Add(header.Key, header.Value);
        }

        if (request.Content != null && request.Content.Headers.ContentType != null)
        {
            var requestBody = await request.Content.ReadAsStringAsync();
            var mediaType = request.Content.Headers.ContentType.MediaType;
            result.Content = new StringContent(requestBody, Encoding.UTF8, mediaType);
            foreach (var header in request.Content.Headers)
            {
                if (!header.Key.Equals("Content-Type", StringComparison.OrdinalIgnoreCase))
                {
                    result.Content.Headers.Add(header.Key, header.Value);
                }


   }
    }

    return result;
}
}

为此,您可以使用xamarin设置插件 ,请按照以下步骤操作。

  1. 安装设置插件。
  2. 添加设置类。
public static class SettingsHelper
    {

        private static ISettings AppSettings
        {
            get
            {
                return CrossSettings.Current;
            }
        }

        #region Setting Constants

        private const string LoginKey = "login_key";
        private static readonly string SettingsDefault = string.Empty;

        #endregion


        public static string GeneralSettings
        {
            get
            {
                return AppSettings.GetValueOrDefault(LoginKey, SettingsDefault);
            }
            set
            {
                AppSettings.AddOrUpdateValue(LoginKey, value);
            }
        }
    }
  1. 在登录方法内部,将值设置为GeneralSettings,然后在下次打开应用程序时检查常规设置值,并相应地分配导航。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM