2017-04-22 88 views
0

我已經使用.NET創建了WebApi。 我想確保一些方法。 我想只有在aspnetuser表的用戶可以執行該方法具有基本授權的webapi

我創建爲

public class BasicAuthHttpModule : IHttpModule

,並在web.config我配置這是

<system.webServer> 
 
    <modules> 
 
     <add name="BasicAuthHttpModule" type="WebHostBasicAuth.Modules.BasicAuthHttpModule"/> 
 
    </modules>  
 
</system.webServer>

而且我BasicAuthHttpModule.cs: 我檢查電子郵件和密碼塔布拉

using System; 
 
using System.Collections.Generic; 
 
using System.Linq; 
 
using System.Net.Http.Headers; 
 
using System.Security.Principal; 
 
using System.Text; 
 
using System.Threading; 
 
using System.Web; 
 
using Microsoft.AspNet.Identity; 
 
using Microsoft.AspNet.Identity.EntityFramework; 
 
using Microsoft.AspNet.Identity.Owin; 
 

 
using Microsoft.Owin; 
 
using ServicioProx360.Models; 
 
using ServicioProx360; 
 

 

 
namespace WebHostBasicAuth.Modules 
 
\t { 
 
\t public class BasicAuthHttpModule: IHttpModule 
 
\t \t { 
 
\t \t private const string Realm = "WebAPI Authentication"; 
 

 
\t \t public void Init(HttpApplication context) 
 
\t \t \t { 
 
\t \t \t // Se registran los manejadores de los eventos 
 
\t \t \t context.AuthenticateRequest += OnApplicationAuthenticateRequest; 
 
\t \t \t context.EndRequest += OnApplicationEndRequest; 
 
\t \t \t } 
 

 
\t \t private static void SetPrincipal(IPrincipal principal) 
 
\t \t \t { 
 
\t \t \t Thread.CurrentPrincipal = principal; 
 
\t \t \t if (HttpContext.Current != null) 
 
\t \t \t \t { 
 
\t \t \t \t HttpContext.Current.User = principal; 
 
\t \t \t \t } 
 
\t \t \t } 
 

 
\t \t private static bool AuthenticateUser(string credentials) 
 
\t \t \t { 
 
\t \t \t var encoding = Encoding.GetEncoding("iso-8859-1"); 
 
\t \t \t credentials = encoding.GetString(Convert.FromBase64String(credentials)); 
 

 
\t \t \t var credentialsArray = credentials.Split(':'); 
 
\t \t \t var username = credentialsArray[0]; 
 
\t \t \t var password = credentialsArray[1]; 
 

 
\t \t \t var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
 

 
\t \t \t ApplicationUser usuario = manager.Find(username, password); 
 
\t \t \t /* Aquí se validan las credenciales o el token enviado en el encabezado de la solicitud */ 
 
// \t \t \t if (!(username == "test" && password == "test")) 
 
\t \t \t if (usuario==null) 
 
\t \t \t \t { 
 
\t \t \t \t return false; 
 
\t \t \t \t } 
 
\t \t \t 
 
\t \t \t var identity = new GenericIdentity(usuario.UserName); 
 
\t \t \t 
 
\t \t \t SetPrincipal(new GenericPrincipal(identity, new string[] { "CIUDADADNO" })); 
 

 
\t \t \t return true; 
 
\t \t \t } 
 

 
\t \t private static void OnApplicationAuthenticateRequest(object sender, EventArgs e) 
 
\t \t \t { 
 
\t \t \t var request = HttpContext.Current.Request; 
 
\t \t \t var authHeader = request.Headers["Authorization"]; 
 
\t \t \t if (authHeader != null) 
 
\t \t \t \t { 
 
\t \t \t \t var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader); 
 

 
\t \t \t \t // Se valida si el esquema de autenticación es básico (Basic Authentication) 
 
\t \t \t \t if (authHeaderVal.Scheme.Equals("basic", StringComparison.OrdinalIgnoreCase) && authHeaderVal.Parameter != null) 
 
\t \t \t \t \t { 
 
\t \t \t \t \t AuthenticateUser(authHeaderVal.Parameter); 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t } 
 

 
\t \t // Si la solicitud no fue aprobada, se agrega el encabezado WWW-Authenticate a la respuesta 
 

 
\t \t private static void OnApplicationEndRequest(object sender, EventArgs e) 
 
\t \t \t { 
 
\t \t \t var response = HttpContext.Current.Response; 
 
\t \t \t if (response.StatusCode == 401) 
 
\t \t \t \t { 
 
\t \t \t \t response.Headers.Add("WWW-Authenticate", string.Format("Basic realm=\"{0}\"", Realm)); 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t public void Dispose() 
 
\t \t \t { 
 
\t \t \t } 
 
\t \t } 
 
\t }

的aspnetusers但doen't做工精細。如果方法是[授權],它要求我提供電子郵件和密碼,我提供了正確的數據,但它再次提出要求。

你能幫我嗎?

非常感謝你

回答