0

I have completed web part of my Project and deployed my website on azure. My project is designed on Web API Entity Framework. Now I need to design Mobile App of my project Cross Platform Xamarin. I know basics of Xamarin development but having difficulties in Login part. Following this tutorial I have done the followings

  1. ApiServices.cs

    public async Task LoginAsync(string userName, string password)
    {
        var keyValues = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("username",userName),
            new KeyValuePair<string, string>("password",password),
            new KeyValuePair<string, string>("grant_type","password"),
        };
        var request = new HttpRequestMessage(HttpMethod.Post, "http://epolleasy.azurewebsites.net/Token");
    
        request.Content = new FormUrlEncodedContent(keyValues);
        var client = new HttpClient();
        var response = await client.SendAsync(request);
        var content = await response.Content.ReadAsStringAsync();
        Debug.WriteLine(content);
    }  
    
  2. LoginViewModel.cs

    public class LoginViewModel {
    
    private ApiServices _apiServices = new ApiServices();
    public string Username { get; set; }
    public string Password { get; set; }
    public ICommand LoginCommand
    {
        get
        {
            return new Command(async() =>
            {
                await _apiServices.LoginAsync(Username, Password);
            });
        }
    }
    }
    
  3. LoginPage.xaml

    <StackLayout>
    <Entry Text="{Binding Username}" Placeholder="Username"/>
    <Entry Text="{Binding Password}" IsPassword="True" Placeholder="Password"/>
    <Button Command="{Binding LoginCommand}" Text="Login"/>
    <Button Text="Signup" Clicked="Button_OnClicked"/>
    </StackLayout>
    

The problem is, this all only generates a token just as shown in tutorial. I want to know what i need to do with this token or how can I get user to login in my application using my builtin project web APIs. I searched a lot but unable to find any good tutorial regarding this topic.

1
  • remember to use httpS since your password is plain-text Commented May 18, 2017 at 12:11

2 Answers 2

1

Its probably a CSRF Token. PAss this in your Headers along with any further requests. The CSRF has a lifetime and is basically alive for that one session.

For further info see https://developer.xamarin.com/guides/xamarin-forms/enterprise-application-patterns/authentication-and-authorization/

Sign up to request clarification or add additional context in comments.

Comments

0

I mobile up on successful login you will get token as you mentioned. You have to save this token and pass it as header when you call other API services.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.