Created
February 25, 2015 01:47
-
-
Save cleytonferrari/b0ff5eb8597cbc644c2a to your computer and use it in GitHub Desktop.
Exemplo claims
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Security.Claims; | |
using System.Web; | |
using Microsoft.AspNet.Identity; | |
using Microsoft.Owin; | |
using Microsoft.Owin.Security; | |
using W7Gestao.Dominio; | |
using W7Gestao.Dominio.Configuracao; | |
using Microsoft.Owin.Infrastructure; | |
namespace W7Gestao.UI.Helper.Acesso | |
{ | |
public static class Seguranca | |
{ | |
public static void SignInCookie(Usuario usuario) | |
{ | |
var identity = ClaimsIdentity(usuario, DefaultAuthenticationTypes.ApplicationCookie); | |
HttpContext.Current.Request.GetOwinContext().Authentication.SignIn(new AuthenticationProperties { IsPersistent = false }, identity); | |
} | |
/* public static dynamic SignInBearer(Usuario usuario) | |
{ | |
var identity = ClaimsIdentity(usuario, Startup.OAuthBearerOptions.AuthenticationType); | |
var ticket = new AuthenticationTicket(identity, new AuthenticationProperties()); | |
var currentUtc = new SystemClock().UtcNow; | |
ticket.Properties.IssuedUtc = currentUtc; | |
ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(60)); | |
var token = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(ticket); | |
return new | |
{ | |
usuario = new | |
{ | |
usuario.Id, | |
usuario.UnidadeId, | |
usuario.Nome, | |
usuario.ArquivoId, | |
usuario.Permissoes | |
}, | |
token | |
}; | |
} | |
*/ | |
private static ClaimsIdentity ClaimsIdentity(Usuario usuario, string authenticationType) | |
{ | |
var claims = new List<Claim> | |
{ | |
new Claim("Nome", usuario.Nome), | |
new Claim("UnidadeId", usuario.UnidadeId), | |
new Claim("UsuarioId", usuario.Id), | |
new Claim("ArquivoId", usuario.ArquivoId) | |
}; | |
claims.AddRange(usuario.Permissoes.Select(permissao => new Claim(ClaimTypes.Role, permissao))); | |
var identity = new ClaimsIdentity(claims, authenticationType); | |
return identity; | |
} | |
public static void SignOut() | |
{ | |
HttpContext.Current.Request.GetOwinContext().Authentication.SignOut(); | |
} | |
public static bool UserIsInRole(string role) | |
{ | |
var ctx = (OwinContext)HttpContext.Current.Request.GetOwinContext(); | |
return ctx.Authentication.User.IsInRole(role); | |
} | |
public static Usuario GetUser() | |
{ | |
var ctx = (OwinContext)HttpContext.Current.Request.GetOwinContext(); | |
var user = ctx.Authentication.User; | |
var usuario = new Usuario | |
{ | |
Nome = GetUserValue("Nome"), | |
UnidadeId = GetUserValue("UnidadeId"), | |
Id = GetUserValue("UsuarioId"), | |
ArquivoId = GetUserValue("ArquivoId") | |
}; | |
var permissoes = new List<string>(); | |
foreach (var claim in user.Claims) | |
{ | |
if (claim.Type == ClaimTypes.Role) | |
{ | |
permissoes.Add(claim.Value); | |
} | |
} | |
usuario.Permissoes = permissoes.ToArray(); | |
return usuario; | |
} | |
public static string GetUserId() | |
{ | |
return GetUserValue("UsuarioId"); | |
} | |
public static string GetUserUnidadeId() | |
{ | |
return GetUserValue("UnidadeId"); | |
} | |
private static string GetUserValue(string key) | |
{ | |
var ctx = (OwinContext)HttpContext.Current.Request.GetOwinContext(); | |
var user = ctx.Authentication.User; | |
if (user == null) | |
return string.Empty; | |
return user.FindFirst(key) == null ? string.Empty : user.FindFirst(key).Value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment