2016-04-05 15 views
0

ASP.NET MVC 3 ile özet kimlik doğrulaması gerçekleştirmem gerekiyor. Bu amaçla AuthorizeAttribute ve HttpUnauthorizedResult öğesinden miras aldım. kod aşağıdaki gibidir:HttpUnauthorizedResult türetilmiş sınıfında bulunmayan ExecuteResult yöntemi

public class DefaultController: Controller { 
    [SessionAuthorize] 
    public ViewResult Index() { 
     return View(); 
    } 
} 

yüzden özel bir şey yapmaz şu şekildedir: kontrolör/eylem için

[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] 
public class SessionAuthorize: AuthorizeAttribute { 
    public override void OnAuthorization (AuthorizationContext actionContext) { 
     try { 
      if (null != actionContext.HttpContext.Request.Headers["Authorization"]) 
       // authorization is on the way 
       // <...> 
      else 
       actionContext.Result = new HttpDigestUnauthorizedResult(); 
     } catch (Exception ex) { 
      Trace.TraceWarning ("SessionAuthorize.OnAuthorization failed: {0}", ex.Message); 
     } 
     base.OnAuthorization (actionContext); 
    } 
} 

public class HttpDigestUnauthorizedResult: HttpUnauthorizedResult { 
    public HttpDigestUnauthorizedResult() : base() { 
    } 
    public override void ExecuteResult (ControllerContext context) { 
     if (context == null) 
      throw new ArgumentNullException ("context"); 
     // this is supposed to initialize digest authentification exchange 
     context.HttpContext.Response.AddHeader ("WWW-Authenticate", string.Format ("Digest realm=\"somerealm\",qop=\"auth\",nonce=\"{0}\",opaque=\"{1}\""/*, <...>*/)); 
     base.ExecuteResult (context); 
    } 
} 

Kod olduğunu. Ancak, ExecuteResult geçersiz kılındı ​​ancak hiçbir zaman yalnızca standart 401 sayfa döndürülür. Burada neyi özlüyorum? ExecuteResult nereden aranmalıdır?

cevap

2

Doğru desen: geçerli isteğin geçerli olup olmadığını öğrenmek için AuthorizeCore (bool döndürür) kullanın ve HandleUnauthorizedRequest yönteminde bu izinsiz istekleri işleyin.'a göre 'a göre her şeyi koymak yanlıştır, bazı durumlarda OnAuthorization yerine OnCacheAuthorization yöntemi çağrılır.

protected override bool AuthorizeCore(HttpContextBase httpContext) 
{ 
    if (httpContext.Request.Headers["Authorization"] == null) 
    { 
     return false; 
    } 

    return base.AuthorizeCore(httpContext); 
} 

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
{ 
    filterContext.Result = new HttpDigestUnauthorizedResult(); 
} 
+0

Teşekkürler, sorunun tam olarak nerede olduğuydu. –

İlgili konular