được rồi vì vậy tôi figured it out nhưng giải pháp có thể là một chút ghetto. Tôi đã lấy AuthorizeAttribute từ nguồn mvc .net và mã hóa phương thức OnAutorization. Điều này chắc chắn làm việc cho tôi tuy nhiên nó chỉ hoạt động cho xác thực cơ bản và tôi không chắc chắn nếu đây là phương pháp an toàn nhất để sử dụng. Tuy nhiên nó giải quyết được vấn đề của các khách hàng web có thể truy cập các dịch vụ an toàn .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();
}
}
}
Nguồn
2009-09-11 14:14:56
Bạn gặp lỗi? Mã trạng thái là gì? – David