2014-09-12 2 views
0

을의 주장을 실패취급이 같은 낸시의 RequiresClaims 메커니즘을 사용하고 낸시

public class HomeModule : NancyModule 
{ 
    public HomeModule() 
    { 
     Get["/"] = ctx => "<a href=\"/admin\">Go here</a>"; 

     Get["/admin"] = ctx => 
     { 
      this.RequiresClaims(new[] { "boss" }); // this 
      return "Hello!"; 
     }; 

     Get["/login"] = ctx => "<form action=\"/login\" method=\"post\">" + 
      "<button type=\"submit\">login</button>" + 
      "</form>"; 

     Post["/login"] = ctx => 
     { 
      return this.Login(Guid.Parse("332651DD-A046-4489-B31F-B6FA1FB290F0")); 
     }; 
    } 
} 

사용자가 사용자가 없기 때문에 /admin를 입력하도록 허가되어 있지 않은 경우 문제는 주장 boss, 낸시 단지 http 상태 403 및 공백 본문으로 응답합니다.

이것은 정확히 내 응용 프로그램의 웹 서비스 부분에 필요한 것이지만, nancy가 사용자를위한 페이지를 구성해야하는 응용 프로그램의 일부도 있습니다. 어떻게 사용자에게보다 유익한 정보를 보여줄 수 있습니까?

내가 사용하는 사용자 매퍼입니다 :

public class MyUserMapper : IUserMapper 
{ 
    public class MyUserIdentity : Nancy.Security.IUserIdentity 
    { 
     public IEnumerable<string> Claims { get; set; } 
     public string UserName { get; set; } 
    } 

    public Nancy.Security.IUserIdentity GetUserFromIdentifier(Guid identifier, Nancy.NancyContext context) 
    { 
     return new MyUserIdentity { UserName = "joe", Claims = new[] { "peon" } }; 
    } 
} 

을 그리고 이것은 내가 사용하는 부트 스트 래퍼입니다 :

public class MyNancyBootstrapper : DefaultNancyBootstrapper 
{ 
    protected override void RequestStartup(
     Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines, NancyContext context) 
    { 
     base.RequestStartup(container, pipelines, context); 

     var formAuthConfig = new Nancy.Authentication.Forms.FormsAuthenticationConfiguration 
     { 
      RedirectUrl = "~/login", 
      UserMapper = container.Resolve<Nancy.Authentication.Forms.IUserMapper>() 
     }; 

     Nancy.Authentication.Forms.FormsAuthentication.Enable(pipelines, formAuthConfig); 
    } 
} 

답변