Month: July 2013

svn:ignore property for Android Development

bin
gen

References:

Advertisements

svn:ignore property for ASP.NET Development

*.o
*.lo
*.la
#*#
.*.rej
*.rej
.*~
*~
.#*
.DS_Store
thumbs.db
Thumbs.db
*.bak
*.class
*.exe
*.dll
*.mine
*.obj
*.ncb
*.lib
*.log
*.idb
*.pdb
*.ilk
*.msi*
.res
*.pch
*.suo
*.exp
*.*~
*.~*
~*.*
cvs
CVS
.CVS
.cvs
release
Release
debug
Debug
ignore
Ignore
bin
Bin
obj
Obj
*.csproj.user
*.user
*.generated.cs
*.Publish.xml
pkg
pkgobj
.vs

References:

Forms Authentication in ASP.NET MVC

1. Edit ~\web.config to include the following forms-based authentication configuration.

<system.web>
	<authentication mode="Forms">
		<forms loginUrl="~/Account/LogOn" timeout="30" />
	</authentication>
</system.web>

2. Register AuthorizeAttribute in ~\App_Start\FilterConfig.cs.

filters.Add(new AuthorizeAttribute());

3. Add view model LogOnViewModel in ~\Models\Account\LogOnViewModel.cs.

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; }

	[Display(Name = "Remember me?")]
	public bool RememberMe { get; set; }
}

4. Add controller AccountController and LogOn action methods for both HttpGet & HttpPost.

public class AccountController : Controller
{
	//
	// GET: /Account/LogOn
	[AllowAnonymous]
	public ActionResult LogOn()
	{
		LogOnViewModel model = new LogOnViewModel();

		return View(model);
	}

	//
	// POST: /Account/LogOn
	[AllowAnonymous]
	[HttpPost]
	public ActionResult LogOn(LogOnViewModel model, string returnUrl)
	{
		if (this.ModelState.IsValid && Membership.ValidateUser(model.UserName, model.Password))
		{
			FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
			if (this.Url.IsLocalUrl(returnUrl))
			{
				return Redirect(returnUrl);
			}
			else
			{
				return RedirectToAction("Index", "Home");
			}
		}

		// If we got this far, something failed, redisplay form
		this.ModelState.AddModelError("", "Incorrect user name or password.");
		return View(model);
	}

	//
	// POST: /Account/LogOff
	[HttpPost]
	public ActionResult LogOff()
	{
		FormsAuthentication.SignOut();

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

5. Add view in ~\Views\Account\LogOn.cshtml.

@model SecurityApp.Models.Account.LogOnViewModel
@{
    Layout = null;
    ViewBag.Title = "Log On";

    ViewBag.ReturnUrl = Request["ReturnUrl"];
}
<!DOCTYPE html>
<html>
......
<body>
    <h2>@ViewBag.Title</h2>
    @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 />
        @Html.CheckBoxFor(m => m.RememberMe)
        @Html.DisplayNameFor(m => m.RememberMe)<br />
        <button type="submit">Log On</button>
    }

    ......
</body>
</html>

6. In ~\Views\Shared\_Layout.cshtml, add a HTML form to handle log off.

@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logOffForm" }))
{
	@Html.AntiForgeryToken()
}

7. In ~\Views\Shared\_Layout.cshtml, add a hyperlink to log off.

<a href="javascript:$('#logOffForm').submit()">Log Off</a>

References

ASP.NET MVC SiteMapPath using Site Map Provider & Bootstrap Breadcrumbs

1. Install Bootstrap for MVC 4 NuGet package. This package will install Twitter Bootstrap to the project and add bundling and minification to application start.

2. Install MvcSiteMapProvider NuGet package. This package is a SiteMapProvider implementation for the ASP.NET MVC framework.

3. In web.config, downsize the MvcSiteMapProvider configuration to the minimal. Keep it simple, stupid. (KISS)

<system.web>
  <siteMap defaultProvider="MvcSiteMapProvider">
    <providers>
      <clear />
      <add name="MvcSiteMapProvider" type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider" siteMapFile="~/Mvc.Sitemap" />
    </providers>
  </siteMap>
</system.web>

4. Modify the ~\Mvc.sitemap file as the following.

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0"
            xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0 MvcSiteMapSchema.xsd"
            enableLocalization="true">
  <mvcSiteMapNode title="Home" controller="Home" action="Index">
    <mvcSiteMapNode title="About" controller="Home" action="About" />
    <mvcSiteMapNode title="Contact" controller="Home" action="Contact" />
    <mvcSiteMapNode title="Administration" clickable="false">
      <mvcSiteMapNode title="User Mgmt" controller="Home" action="UserMgmt" />
      <mvcSiteMapNode title="Role Mgmt" controller="Home" action="RoleMgmt" />
    </mvcSiteMapNode>
    <mvcSiteMapNode title="Profile" clickable="false">
      <mvcSiteMapNode title="Change Password" controller="Home" action="ChangePassword" />
      <mvcSiteMapNode title="Separator" clickable="false" />
      <mvcSiteMapNode title="Sign Off" controller="Home" action="SignOff" />
    </mvcSiteMapNode>
  </mvcSiteMapNode>
</mvcSiteMap>

5. The Twitter Bootstrap package installed in step 1 wouldn’t add the style and script rendering to the layout file. You need to do it manually by modifying the _Layout.cshtml file as following.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width" />
  <title>@ViewBag.Title</title>
  @Styles.Render("~/Content/css")
  @Styles.Render("~/Content/bootstrap")
  @Scripts.Render("~/bundles/modernizr")
</head>
<body>
  @RenderBody()

  @Scripts.Render("~/bundles/jquery")
  @Scripts.Render("~/bundles/bootstrap")
  @RenderSection("scripts", required: false)
</body>
</html>

6. Add a partial view file BootstrapSiteMapPathHelperModel.cshtml under ~\Views\Shared\DisplayTemplates to render the Site Map Path which fits Bootstrap’s Breadcrumbs specifications.

@model MvcSiteMapProvider.Web.Html.Models.SiteMapPathHelperModel
@using System.Web.Mvc.Html
@using System.Linq
@using MvcSiteMapProvider.Web.Html.Models

@if (Model.Nodes.Count != 1)
{
    @:
<ul class="breadcrumb">
foreach (var node in Model.Nodes)
{
    if (node == Model.Nodes.First())
    {
        continue;
    }

    if (node != Model.Nodes.Last())
    {
        string url = node.IsClickable ? node.Url : "#";

            @:
	<li><a href="@url">@node.Title</a><span class="divider">></span></li>
}
    else
    {
            @:
	<li class="active">@node.Title</li>
}
}

    @:</ul>
}

7. Add HomeController, and then add Index, About, Contact, UserMgmt, RoleMgmt & ChangePassword Views.

8. Edit the _Layout.cshtml file again to render the MvcSiteMap’s site map path using the Bootstrap site map path partial view.

......
<body>
	<div class="container">
		<div class="row">
			<div class="span12">
				<nav>
					@Html.MvcSiteMap().Menu("BootstrapMenuHelperModel")
				</nav>
			</div>
		</div>
		<!-- //row -->
		<div class="row">
			<div class="span12">
				@Html.MvcSiteMap().SiteMapPath("BootstrapSiteMapPathHelperModel")
			</div>
		</div>
		<!-- //row -->
		<div class="row">
			<div class="span12">
				@RenderBody()
			</div>
		</div>
		<!-- //row -->
	</div>
	......
</body>
......

9. Completed!!!

About Site Map Path
About Site Map Path

Administration > User Mgmt Site Map Path
Administration > User Mgmt Site Map Path

Profile > Change Password Site Map Path
Profile > Change Password Site Map Path

You can download the code sample here.

References