The Miracle
The Miracle

Reputation: 29

Xamarin User Authentication using AD B2C

So I tried this sample for user authentication in xamarin using ad b2c https://dev.to/425show/user-authentication-in-xamarin-with-azure-ad-b2c-4clm

But after user login it goes back to login page that's having the login Button in xamarin instead of the LoginResult page where the user info is displayed. After several tries for some hours, it then works as expected (I.e it navigates to LoginResult page). This happens whenever I just deploy the app to my Android device. Pls what could be the problem and to fix it. Thanks

Upvotes: 0

Views: 174

Answers (1)

Kartik Bhiwapurkar
Kartik Bhiwapurkar

Reputation: 5159

• To fix this issue of redirecting the successful user login to the LoginResult page rather than redirecting the user to the original login page again, you would have to edit some configuration files in your Xamarin application. They are as below: -

a) Replace the order of items in AppShell.xaml file by downloading the same from https://github.com/davidortinau/ShellLoginSample/find/master with the format as below: -

<!--  Your Pages  -->
<TabBar Route="main">
<Tab Title="Browse" Icon="tab_feed.png">
    <ShellContent ContentTemplate="{DataTemplate local:ItemsPage}" />
</Tab>
<Tab Title="About" Icon="tab_about.png">
    <ShellContent ContentTemplate="{DataTemplate local:AboutPage}" />
</Tab>
</TabBar>

<ShellItem Route="login">
<ShellContent ContentTemplate="{DataTemplate local:LoginPage}" />
</ShellItem>

b) Then edit the App.xaml.cs file as below to check the login state: -

  public App()
 {
   InitializeComponent();
   DependencyService.Register<MockDataStore>();
   var isLoogged = Xamarin.Essentials.SecureStorage.GetAsync("isLogged").Result;
if (isLoogged == "1")
{
    MainPage = new AppShell();
}
else
{
    MainPage = new LoginPage();
}
}

c) And finally edit the LoginPage.xaml.cs file as below: -

 protected async void OnClicked(object source, EventArgs args)
 {
await Xamarin.Essentials.SecureStorage.SetAsync("isLogged", "1");
Application.Current.MainPage = new AppShell();
await Shell.Current.GoToAsync("//main");
  }

Thus, in this way by making modifications to the Xamarin app’s files, you should get rid of the post login redirection issue. For more details regarding this, kindly refer to the link below: -

What's the correct way to implement login page in Xamarin Shell?

Upvotes: 1

Related Questions