Azure Tips and Tricks TIP
💡 Learn more : App Service Documentation.
When I'm building out a website with MVC5 and Azure, it typically lands on *.azurewebsites.net and generally I don't need any user authentication. But if I need it, I typically need 1 administrator account and 0 users. So why didn't I just go to Settings -> Authentication/Authorization and turn on AAD or create a gmail, twitter, etc. login? Turns out that I could have set something like that up (after spending time researching how), but I really just needed a layer of authentication for myself (the one and only administrator) and prevent anyone else without that password to my site. I didn't want to use any of the built-in authentications methods of ASP.NET either as I didn't want/need a database to maintain.
Part 1:
<authentication mode="Forms')">
<forms loginUrl="~/Account/LogOn" timeout="30"/>
</authentication>
Note: Make sure you are using the root web.config.
Part 2:
~\App_Start\FilterConfig.cs.filters.Add(new AuthorizeAttribute());
Part 3:
Models\Account\LogOnViewModel.cs.using System.ComponentModel.DataAnnotations;
namespace MVCMobileApp.Models.Account
{
public class LogOnViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
}
}
Part 4:
public class AccountController : Controller
{
[AllowAnonymous]
public ActionResult LogOn()
{
LogOnViewModel model = new LogOnViewModel();
return View(model);
}
[AllowAnonymous]
[HttpPost]
public ActionResult LogOn(LogOnViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (model.UserName == CloudConfigurationManager.GetSetting("UName") && model.Password == CloudConfigurationManager.GetSetting("UPw"))
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Incorrect username or password");
}
}
return View(model);
}
public ActionResult LogOff()
{
Request.Cookies.Remove("UserName");
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
}
Part 5:
Part 6:
Part 7:
Views\Account<meta name="viewport" content="width=device-width, initial-scale=1.0')">
@model MVCMobileApp.Models.Account.LogOnViewModel
@{
Layout = null;
ViewBag.Title = "Log On";
ViewBag.ReturnUrl = Request["ReturnUrl"];
}
<div class="login')">
@using (Html.BeginForm(null, null, new { returnUrl = ViewBag.ReturnUrl }, FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)<br />
@Html.TextBoxFor(m => m.UserName, new { placeholder = Html.DisplayNameFor(m => m.UserName) })<br />
@Html.PasswordFor(m => m.Password, new { placeholder = Html.DisplayNameFor(m => m.Password) })<br />
<button type="submit" class="btn btn-primary btn-block btn-large')">Log On</button>
}
</div>
Part 8:
On the page that I want to protect (for example my Index page in Home) on the controller I'd do the following:
[Authorize]
public class HomeController : Controller
...
Part 9:
Add a Sign Out Action link inside the _Layouts.cshtml in the Shared folder.
<li>@Html.ActionLink("Log Off", "LogOn", "Account", null, new { @class = "actnclass" })</li>
Nice! Our single user authentication is now in place. See the quick demo below and keep in mind that this really is for quick and dirty user authencation.
