Skip to content

Instantly share code, notes, and snippets.

@sethdorris
Created December 8, 2016 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sethdorris/92660b0f0248123b2a9144284de03239 to your computer and use it in GitHub Desktop.
Save sethdorris/92660b0f0248123b2a9144284de03239 to your computer and use it in GitHub Desktop.
@using Microsoft.AspNet.Identity
@using PS_Esports.Utils
@using System.Security.Claims
@using System.Security.Principal
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Hello " + @User.Identity.GetTag() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
}
}
else
{
<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Tag = model.tag};
var person = new Person(model.tag);
_context.Persons.Add(person);
_context.SaveChanges();
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
UserManager.AddClaim(user.Id, new Claim("Tag", user.tag));
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Web;
using Microsoft.AspNet.Identity;
namespace PS_Esports.Utils
{
public static class ClaimsPrincipleExtension
{
public static string GetTag(this IIdentity identity) {
var claim = ((ClaimsIdentity) identity).Claims.Where(o => o.Type == "Tag").FirstOrDefault();
return (claim != null) ? claim.Value : string.Empty;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment