简体   繁体   中英

How to save log-in state in Windows Phone 8?

I'm trying to build an app for WP8 with a login page. I want to save the user login state so that the end-user will not have to retype his/her credentials, my app is a HTML page embedded in the webbrowser control. Someone Can help me how save login state ?

First of all if the website you are trying to log in to is not yours then what you ask for cannot be done.

Now assuming that you are using a WebBrowser in your XAML code and that the website you log in to is yours lets say you have a login successfull method:

there you have to call this code:

window.external.notify("LogInSuccess");

Then your WebBrowser should be like this:

<phone:WebBrowser HorizontalAlignment="Stretch" Name="webBrowserControl" VerticalAlignment="Stretch" IsScriptEnabled="True" ScriptNotify="JavaScriptNotify"/>

And the method that gets called in .cs file should actually store a success value into isolated storage that will be checked every time you open the login page:

void JavaScriptNotify(Object sender, NotifyEventArgs notifyArgs)
{
    // Check if the value is correct:
    if (notifyArgs.Value.Equals("LogInSuccess"))
    {
        // Save State to Isolated Storage
        var settings = IsolatedStorageSettings.ApplicationSettings;
        settings.Add("loginStatus ", "success");
    }
}

Finally you should override the constructor method of your login page to the following:

public LoginPage() { var loginStatus = settings["loginStatus "].ToString(); if (loginStatus.Equals(success)) { NavigationService.Navigate(new Uri("/PageAfterLoginPage.xaml, UriKind.Relative)); } }

And for this to be complete override the OnNavigatedTo method of PageAfterLoginPage.xaml and add the following code:

NavigationService.RemoveBackEntry();

Last part will just remove the login page from the stack so that when the user clicks the back button wont be navigated to that screen again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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