GithubHelp home page GithubHelp logo

sergepilipchuk / maui-authenticate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from devexpress-examples/maui-authenticate

0.0 0.0 0.0 577 KB

This example shows how to use our free .NET App Security Library & Web API Service to implement authentication in your .NET MAUI application.

License: Other

C# 80.22% HTML 19.78%

maui-authenticate's Introduction

Authenticate Users with the DevExpress Web API Service

This example demonstrates how to use our free .NET App Security & Web API Service to implement user authentication based on simple, form-based authentication logic (login/password). A wizard helps you generate a ready-to-use authentication service. This service uses the Entity Framework Core ORM to access a database.

You can find more information about our Web API Service's access restrictions in the following resources:

Article: Create a Standalone Web API Application

Video: A 1-Click Solution for CRUD Web API with Role-based Access Control via EF Core & ASP.NET

Prerequisites

SQL Server, if you run this solution on Windows.

Run Projects

  1. Open the WebAPI solution. Run Visual Studio as an administrator so that the IDE can create the database as defined in appsettings.json.

  2. Choose the WebApi item in the debug dropdown menu. This selection will debug the project on the Kestrel web server.

    Run Settings

    If you prefer IIS Express to Kestrel, select IIS Express from the debug drop-down menu, and use an external text editor to add the following code to .vs\MAUI_WebAPI\config\applicationhost.config:

    <sites>
        <site name="WebSite1" id="1" serverAutoStart="true">
        <!-* ... -->
            <bindings>
                <binding protocol="http" bindingInformation="*:65201:*" />
                <binding protocol="https" bindingInformation="*:44317:*" />
                <binding protocol="https" bindingInformation="*:44317:localhost" />
                <binding protocol="http" bindingInformation="*:65201:localhost" />
            </bindings>
        </site>
        <!-* ... -->
    </sites>
  3. Right-click the MAUI project, choose Set as Startup Project, and select your emulator. Note that physical devices that are attached over USB do not allow you to access your machine's localhost.

  4. Right-click the WebAPI project and select Debug > Start new instance.

  5. Right-click the MAUI project and select Debug > Start new instance.

Implementation Details

Service and Communication

  • DevExpress Web API Service uses JSON Web Tokens (JWT) to authorize users. Call WebAPI's Authenticate endpoint and pass a username and password to the endpoint from the .NET MAUI application. In this example, token generation logic is implemented in the WebAPIService.RequestTokenAsync method:

      private async Task<HttpResponseMessage> RequestTokenAsync(string userName, string password) {
            return await HttpClient.PostAsync($"{ApiUrl}Authentication/Authenticate",
                                                new StringContent(JsonSerializer.Serialize(new { userName, password = $"{password}" }), Encoding.UTF8,
                                                ApplicationJson));
      }

    Include the token in HttpClient.DefaultRequestHeaders.Authorization. All subsequent requests can then access private endpoints and data: 

    HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await tokenResponse.Content.ReadAsStringAsync());

    File to Look At: WebAPIService.cs

  • To create users and specify their passwords, use the Updater.UpdateDatabaseAfterUpdateSchema method. You can modify a user's password directly in the database or use the full version of our XAF UI.

    File to Look At: Updater.cs

Login UI and View Model

  • Use the TextEdit.StartIcon and PasswordEdit.StartIcon properties to display icons in the TextEdit and PasswordEdit controls.

    <dxe:TextEdit LabelText="Login" StartIcon="editorsname" .../>
    <dxe:PasswordEdit LabelText="Password" StartIcon="editorspassword" .../>

    File to Look At: LoginPage.xaml

  • To validate the PasswordEdit control's value, use the EditBase.HasError and EditBase.ErrorText inherited properties.

    <dxe:PasswordEdit ... HasError="{Binding HasError}" ErrorText="{Binding ErrorText}"/>

    File to Look At: LoginPage.xaml

    public class LoginViewModel : BaseViewModel {
        // ...
        string errorText;
        bool hasError;
        // ...
    
        public string ErrorText {
            get => errorText;
            set => SetProperty(ref errorText, value);
        }
    
        public bool HasError {
            get => hasError;
            set => SetProperty(ref hasError, value);
        }
        async void OnLoginClicked() {
            /// ...
            string response = await DataStore.Authenticate(userName, password);
            if (!string.IsNullOrEmpty(response)) {
                ErrorText = response;
                HasError = true;
                return;
            }
            HasError = false;
            await Navigation.NavigateToAsync<SuccessViewModel>();
        }
    }

    File to Look At: LoginViewModel.cs

  • Specify the TextEdit.ReturnType inherited property to focus the PasswordEdit control after the TextEdit control's value is edited.

  • Bind the PasswordEdit.ReturnCommand property to the Login command to execute the command when a user enters the password:

    <dxe:PasswordEdit ReturnCommand="{Binding LoginCommand}"/>

    File to Look At: LoginPage.xaml

    public class LoginViewModel : BaseViewModel {
        // ...
        public LoginViewModel() {
            LoginCommand = new Command(OnLoginClicked);
            SignUpCommand = new Command(OnSignUpClicked);
            PropertyChanged +=
                (_, __) => LoginCommand.ChangeCanExecute();
    
        }
        // ...
        public Command LoginCommand { get; }
        public Command SignUpCommand { get; }
        // ...
    }

    File to Look At: LoginViewModel.cs

Debug Specifics

Android emulator and iOS simulator request a certificate to access a service over HTTPS. In this example, we switch to HTTP in debug mode:

#if !DEBUG
    app.UseHttpsRedirection();
#endif

MAUI - Android

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain>
    </domain-config>
</network-security-config>

MAUI - iOS

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsLocalNetworking</key>
    <true/>
</dict>

This allows you to bypass the certificate check without the need to create a development certificate or implement HttpClient handlers.

For more information, please refer to Connect to local web services from Android emulators and iOS simulators.

We recommend that you use HTTP only when you develop/debug your application. In production, use HTTPS for security reasons.

Files to Look At

Documentation

More Examples

maui-authenticate's People

Contributors

friedcucumber avatar sergepilipchuk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.