C
C#2y ago
pyrodistic

❔ Xamarin Authentication

Hello everyone. I currently have a MVC project that uses the IdentityUser base class to register/login users. I'm not manually using any kind of token. Using the same database I wanted to Authenticate the user in Xamarin. How can I say to the Xamarin app that the User/IdentityUser logged is the one the app should keep? Sorry if it appears too broad of a question, but would appreciate any help!
4 Replies
Angius
Angius2y ago
You're probably looking for OAuth or something similar Or something simpler, like an app token Assuming it'll just be your app that uses your API Essentially, you assign each user a random token, and let them use it instead of the password when logging in via the app
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
Would have to store it somewhere
pyrodistic
pyrodistic2y ago
Sorry, regarding API authentication I'm a complete beginner. I didn't use any default Identity option in my MVC project. I am using Jwt for email confirmation on the MVC section. For example on my MVC Startup:
services.AddIdentity<User, IdentityRole>(cfg =>
{
cfg.Tokens.AuthenticatorTokenProvider = TokenOptions.DefaultAuthenticatorProvider;
cfg.SignIn.RequireConfirmedEmail = true;
cfg.User.RequireUniqueEmail = true;
cfg.Password.RequireDigit = false;
cfg.Password.RequiredUniqueChars = 0;
cfg.Password.RequireUppercase = false;
cfg.Password.RequireLowercase = false;
cfg.Password.RequireNonAlphanumeric = false;
cfg.Password.RequiredLength = 6;
})
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<DataContext>();

services.AddAuthentication()
.AddCookie()
.AddJwtBearer(cfg =>
{
cfg.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = this.Configuration["Tokens:Issuer"],
ValidAudience = this.Configuration["Tokens:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(this.Configuration["Tokens:Key"]))
};
});
services.AddIdentity<User, IdentityRole>(cfg =>
{
cfg.Tokens.AuthenticatorTokenProvider = TokenOptions.DefaultAuthenticatorProvider;
cfg.SignIn.RequireConfirmedEmail = true;
cfg.User.RequireUniqueEmail = true;
cfg.Password.RequireDigit = false;
cfg.Password.RequiredUniqueChars = 0;
cfg.Password.RequireUppercase = false;
cfg.Password.RequireLowercase = false;
cfg.Password.RequireNonAlphanumeric = false;
cfg.Password.RequiredLength = 6;
})
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<DataContext>();

services.AddAuthentication()
.AddCookie()
.AddJwtBearer(cfg =>
{
cfg.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = this.Configuration["Tokens:Issuer"],
ValidAudience = this.Configuration["Tokens:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(this.Configuration["Tokens:Key"]))
};
});
On my Login for the MVC:
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var result = await _userHelper.LoginAsync(model);


if (result.Succeeded)
{

return RedirectToAction("Index", "Dashboard");
}
}

this.ModelState.AddModelError(string.Empty, "Failed to login");
return View(model);
}
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var result = await _userHelper.LoginAsync(model);


if (result.Succeeded)
{

return RedirectToAction("Index", "Dashboard");
}
}

this.ModelState.AddModelError(string.Empty, "Failed to login");
return View(model);
}
After this I just use User.Identity to refer to the user logged in. On my API Controller for the Mobile:
public async Task<IActionResult> Login(LoginBindingModel model)
{
if (ModelState.IsValid)
{

var result = await _userHelper.LoginAsync(model);

var user = await _userHelper.GetUserByEmailAsync(model.Username);

if (!result.Succeeded)
{
return BadRequest(new ProblemDetails
{
Title = "Failed to login...",
});
}
return Ok(user);
}

return BadRequest();
}
public async Task<IActionResult> Login(LoginBindingModel model)
{
if (ModelState.IsValid)
{

var result = await _userHelper.LoginAsync(model);

var user = await _userHelper.GetUserByEmailAsync(model.Username);

if (!result.Succeeded)
{
return BadRequest(new ProblemDetails
{
Title = "Failed to login...",
});
}
return Ok(user);
}

return BadRequest();
}
So when I GET this response on the Mobile I want to say to the app that the user received is now the user logged in.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.