Switch to using Identity extensions
This commit is contained in:
parent
c06aa04654
commit
e61107a381
|
|
@ -1,6 +1,4 @@
|
||||||
using System;
|
using Microsoft.AspNet.Identity;
|
||||||
using System.Security.Claims;
|
|
||||||
using Microsoft.AspNet.Identity;
|
|
||||||
using Microsoft.AspNet.Identity.Security;
|
using Microsoft.AspNet.Identity.Security;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
|
|
@ -209,64 +207,4 @@ namespace MusicStore.Controllers
|
||||||
|
|
||||||
#endregion
|
#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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using Microsoft.AspNet.Mvc;
|
using System.Security.Principal;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
|
||||||
|
|
@ -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 System.Security.Principal
|
||||||
@using MusicStore.Controllers
|
|
||||||
@model MusicStore.Models.ManageUserViewModel
|
@model MusicStore.Models.ManageUserViewModel
|
||||||
|
|
||||||
<p>You're logged in as <strong>@Context.HttpContext.User.Identity.GetUserName()</strong>.</p>
|
<p>You're logged in as <strong>@Context.HttpContext.User.Identity.GetUserName()</strong>.</p>
|
||||||
|
|
|
||||||
|
|
@ -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 System.Security.Principal
|
||||||
@using MusicStore.Controllers
|
|
||||||
@using Microsoft.AspNet.Identity;
|
|
||||||
|
|
||||||
@if (Context.HttpContext.User != null && Context.HttpContext.User.Identity.IsAuthenticated)
|
@if (Context.HttpContext.User != null && Context.HttpContext.User.Identity.IsAuthenticated)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue