2014-11-05 4 views
0

사용자 로그인을 위해 WebAPI를 구축했으며 사용자가 올바른 UserName 및 비밀번호를 제공하면 webAPI가 액세스 토큰을 생성 할 수 있습니다. 내 질문은 또한 사용자 역할 정보를 MVC 응용 프로그램에 전달할 수있는 방법입니다. 예를 들어 웹 API를 사용하는 MVC 사용자 인증

,

나는이 웹 API에서 아래 MVC 응용 프로그램 컨트롤러, 어떻게 ', UserEditor를 관리자'역할을 통과 할 수 있습니까? 다른 WebAPI 호출을 사용하여 사용자 역할을 확인할 수는 있지만이를 수행하는 것은 좋지 않습니다.

[Authorized("Admin,UserEditor")] 
ActionResult EditUser(int? Id) 
{ 
........ 
} 

답변

1

당신은 당신이 토큰 및 쿠키를 사용하여 MVC 5 컨트롤러를 사용하여 웹 API의 끝 지점을 확보하고 있기 때문에이 인증 메커니즘 (무기명 토큰 및 쿠키)를 사용합니다. MVC 핵심 종속성이 선택된 VS 2013 웹 템플리트를 선택하는 것이 좋습니다. 그것은 당신의 경우에 필요한 모든 코드가 포함되어 있습니다. 웹 API에 대한 oAuthIdentity이 얼마나

 public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>(); 

     ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password); 

     if (user == null) 
     { 
      context.SetError("invalid_grant", "The user name or password is incorrect."); 
      return; 
     } 

     ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, 
      OAuthDefaults.AuthenticationType); 
     ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager, 
      CookieAuthenticationDefaults.AuthenticationType); 

     AuthenticationProperties properties = CreateProperties(user.UserName); 
     AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); 
     context.Validated(ticket); 
     context.Request.Context.Authentication.SignIn(cookiesIdentity); 
    } 

공지 사항 및 MVC 응용 프로그램에 대한 cookiesIdentity 다음 GrantResourceOwnerCredentials 방법 내부에는 아래에 비슷한 찾을 수 있습니다.

+0

답장 보내 주셔서 감사합니다. GrantResourceOwnerCredentials를 사용자 지정해야하고 쿠키에서 AccessToken을 확인해야합니까? 여기에 다른 MVC 응용 프로그램이 WebAPI에서 소유권 주장 정보를 얻는 방법에 대한 또 다른 질문이 있습니다. WebAPI와 MVC가 같은 도메인에 있지 않으면 컴퓨터 키를 공유해야합니까? – user2376512