Bon alors je me suis dehors, mais la solution peut être un ghetto bits. J'ai pris le AuthorizeAttribute de .net mvc source et recodé la méthode OnAutorization. Cela fonctionne certainement pour moi, mais cela fonctionne seulement pour l'authentification de base et je ne suis pas sûr si c'est la méthode la plus sûre à utiliser. Cependant, cela résout le problème des clients Web qui peuvent accéder à des services de repos sécurisés .net mvc.
public virtual void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
string auth = filterContext.HttpContext.Request.Headers["authorization"];
if (!String.IsNullOrEmpty(auth))
{
byte[] encodedDataAsBytes = Convert.FromBase64String(auth.Replace("Basic ", ""));
string val = Encoding.ASCII.GetString(encodedDataAsBytes);
string userpass = val;
string user = userpass.Substring(0, userpass.IndexOf(':'));
string pass = userpass.Substring(userpass.IndexOf(':') + 1);
if (!System.Web.Security.Membership.Provider.ValidateUser(user, pass))
{
filterContext.Result = new HttpUnauthorizedResult();
}
}
else
{
if (AuthorizeCore(filterContext.HttpContext))
{
HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
cachePolicy.SetProxyMaxAge(new TimeSpan(0));
cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */);
}
else
{
// auth failed, redirect to login page
filterContext.Result = new HttpUnauthorizedResult();
}
}
}
Vous avez une erreur? Quel est le code d'état? – David