@Roles.GetRolesForUser()
면도기 레이아웃보기에서 역할을 반환하지 않습니다. @Roles.GetRolesForUser().Count()
0입니다.
같은 장소에서 같은 뷰로 @Roles.IsUserInRole('name_of_logged_in_role')
돌아 오는 동안 true
.
면도기보기 :
<p>
@User.Identity.Name //Output: MyName
@Roles.GetRolesForUser().Count() //Output: 0
@Roles.IsUserInRole("Radiologist") //Output: True
</p>
최신 정보
@Roles.GetRolesForUser(User.Identity.Name).Length //Output: 0
@Roles.GetRolesForUser(User.Identity.GetUserName()).Length //Output: 0
광범위한 조사 끝에 마침내 문제를 발견했습니다. 웹 애플리케이션에서 문제를 재현 할 수있었습니다. 분명히 ASP.NET Identity 를 Simple Membership 과 결합 할 수는 없습니다 . 단순히 GetRolesForUser
메서드 를 사용했을 때처럼 말입니다 . Roles
객체에 대한 기본 설정입니다 간단한 회원 기본 공급자를 사용하지만, 그것은 당신의 사용처럼 보인다 ASP.NET 정체성 없는 간단한 회원 . 나는 왜 그것이 작동하지 않는지 스스로 궁금해 할 때까지 차이를 알아 차리지 못했습니다.
당신이 얻은 이유 string[0]
는 GetRolesForUser
데이터베이스에 존재하지 않는 테이블에 SQL 쿼리를 실행 했기 때문 입니다.
작동하는 이유 IsUserInRole
는 다소간 기본 공급자를 사용하여 확인하지 않았기 때문입니다.CacheRolesInCookie
If CacheRolesInCookie is true, then roleName may be checked against the roles cache rather than the specified role provider.
따라서 기술적으로는 기본 공급자가 나열한 string[0]
connectionString으로 이동하여 해당 connectionString을 사용하는 데이터베이스에 데이터가 없기 때문에 반환 됩니다. Simple Membership 데이터베이스 스키마가 ASP.NET Identity 와 다르기 때문에 현재 데이터베이스를 공급자에 추가해도 도움이되지 않습니다.
즉, 다음과 같이 UserName으로 역할을 가져와야합니다.
간단한 솔루션 :
public List<string> GetRoles(string UserName)
{ List<string> roles = new List<string>();
if (!string.IsNullOrWhiteSpace(UserName))
{
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var account = new AccountController();
roles = account.UserManager.GetRoles(user.Id);
}
return roles;
}
업데이트 됨
확장 솔루션 :
컨텍스트에서 ASP.NET ID 역할을 확장 할 수 있습니다.
http://www.codeproject.com/Articles/799571/ASP-NET-MVC-Extending-ASP-NET-Identity-Roles
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다