Created
September 12, 2013 16:43
-
-
Save cleytonferrari/6540563 to your computer and use it in GitHub Desktop.
Exemplo de Autenticação em WebAPI algumas referencias http://andrebaltieri.azurewebsites.net/webapi-como-camada-de-serviosparte-3-autenticao/ e outra http://msdn.microsoft.com/pt-br/library/vstudio/y9dd5fx0.aspx
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.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Security.Principal; | |
using System.Text; | |
using System.Threading; | |
using System.Web.Http.Controllers; | |
using System.Web.Http.Filters; | |
using W7Gestao.Dominio.Configuracao; | |
namespace W7Gestao.UI.Web.Helper | |
{ | |
public class Seguranca : ActionFilterAttribute | |
{ | |
public string Roles { get; set; } | |
public override void OnActionExecuting(HttpActionContext actionContext) | |
{ | |
if (actionContext.Request.Headers.Authorization == null) | |
{ | |
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized); | |
} | |
else | |
{ | |
var token = actionContext.Request.Headers.Authorization.Parameter; | |
var tokenDecodificado = Encoding.UTF8.GetString(Convert.FromBase64String(token)); | |
var login = tokenDecodificado.Substring(0, tokenDecodificado.IndexOf(":", StringComparison.Ordinal)); | |
var senha = tokenDecodificado.Substring(tokenDecodificado.IndexOf(":", StringComparison.Ordinal) + 1); | |
var user = ValidarUsuario(login, senha); | |
if (user != null) | |
{ | |
var minhaIdentidade = new GenericIdentity(user.Email); | |
var principal = new GenericPrincipal(minhaIdentidade, user.Permissoes); | |
Thread.CurrentPrincipal = principal; | |
base.OnActionExecuting(actionContext); | |
} | |
else | |
{ | |
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized); | |
} | |
} | |
} | |
private Usuario ValidarUsuario(string login, string senha) | |
{ | |
var permissoes = new string[] { "Administrador" }; | |
if (login == "cleytonferrari" && senha == "123456") | |
{ | |
if (string.IsNullOrEmpty(Roles) || permissoes.Contains(Roles)) | |
return new Usuario() { Email = "cleytonferrari@gmail.com", Permissoes = permissoes }; | |
} | |
return null; | |
} | |
} | |
} |
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.Collections.Generic; | |
using System.Web.Http; | |
using W7Gestao.UI.Web.Helper; | |
namespace W7Gestao.UI.Web.Controllers.API.Configuracao | |
{ | |
public class UsuarioController : ApiController | |
{ | |
[Seguranca(Roles = "Administrador")] | |
public IEnumerable<string> Get() | |
{ | |
if(User.IsInRole("Gerente")) | |
return new[] { "value1", "value2" }; | |
return new[] {User.Identity.Name}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Não seria melhor seguir algo assim:
http://elemarjr.net/2013/08/16/claims-based-access-control-com-claims-no-net-4-5/