Switch to using Identity extensions

This commit is contained in:
Hao Kung 2014-04-17 14:29:25 -07:00
parent c06aa04654
commit e61107a381
4 changed files with 5 additions and 69 deletions

View File

@ -1,6 +1,4 @@
using System;
using System.Security.Claims;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Security;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.ModelBinding;
@ -209,64 +207,4 @@ namespace MusicStore.Controllers
#endregion
}
/// <summary>
/// TODO: Temporary APIs to unblock build. Need to remove this once we have these APIs available.
/// </summary>
public static class Extensions
{
/// <summary>
/// Return the user name using the UserNameClaimType
/// </summary>
/// <param name="identity"></param>
/// <returns></returns>
public static string GetUserName(this IIdentity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
var ci = identity as ClaimsIdentity;
if (ci != null)
{
return ci.FindFirstValue(ClaimsIdentity.DefaultNameClaimType);
}
return null;
}
/// <summary>
/// Return the user id using the UserIdClaimType
/// </summary>
/// <param name="identity"></param>
/// <returns></returns>
public static string GetUserId(this IIdentity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
var ci = identity as ClaimsIdentity;
if (ci != null)
{
return ci.FindFirstValue(ClaimTypes.NameIdentifier);
}
return null;
}
/// <summary>
/// Return the claim value for the first claim with the specified type if it exists, null otherwise
/// </summary>
/// <param name="identity"></param>
/// <param name="claimType"></param>
/// <returns></returns>
public static string FindFirstValue(this ClaimsIdentity identity, string claimType)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
var claim = identity.FindFirst(claimType);
return claim != null ? claim.Value : null;
}
}
}

View File

@ -1,4 +1,5 @@
using Microsoft.AspNet.Mvc;
using System.Security.Principal;
using Microsoft.AspNet.Mvc;
using MusicStore.Models;
using System;
using System.Linq;

View File

@ -1,5 +1,4 @@
@*This using statement to MusicStore.Controllers should go away once we have the GetUserName() extension on IIdentity in some Identity package*@
@using MusicStore.Controllers
@using System.Security.Principal
@model MusicStore.Models.ManageUserViewModel
<p>You're logged in as <strong>@Context.HttpContext.User.Identity.GetUserName()</strong>.</p>

View File

@ -1,6 +1,4 @@
@*This using statement to MusicStore.Controllers should go away once we have the GetUserName() extension on IIdentity in some Identity package*@
@using MusicStore.Controllers
@using Microsoft.AspNet.Identity;
@using System.Security.Principal
@if (Context.HttpContext.User != null && Context.HttpContext.User.Identity.IsAuthenticated)
{