(ASP.NET 아이덴티티 2.1) 다음과 같이 내가 주장을 설정 : 내가 발행 내 클라이언트에서ASP.NET 웹 API 메서드 내에서 클레임 내용을 어떻게 찾을 수 있습니까? 내 코드에서
public static AuthenticationProperties CreateProperties(
string userName,
ClaimsIdentity oAuthIdentity,
string firstName,
string lastName,
int organization)
{
IDictionary<string, string> data = new Dictionary<string, string>
{
{ "userName", userName},
{ "firstName", firstName},
{ "lastName", lastName},
{ "organization", organization.ToString()},
{ "roles",string.Join(":",oAuthIdentity.Claims.Where(c=> c.Type == ClaimTypes.Role).Select(c => c.Value).ToArray())}
};
return new AuthenticationProperties(data);
}
:
여기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,
oAuthIdentity,
user.FirstName,
user.LastName,
user.Organization);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
다음 추가 역할에 대한 주장 CreateProperites 방법 토큰에 대한 요청을 다음과 같이 수신하십시오 :
this.$http({
method: 'POST',
url: '/Token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data: 'grant_type=password&username=' + encodeURIComponent(userName) + '&password=' + encodeURIComponent(password),
})
.success((data: any, status, headers, cfg) => {
self.data.roles = data.roles;
self.data.roles가 역할로 올바르게 채워지는 것을 볼 수 있습니다. 이제 서버에서 롤의 내용을 확인하고 싶습니다. 누군가 이렇게 할 수있는 방법을 말해줌으로써 도움을받을 수 있습니까?
[HttpGet]
[Route("Retrieve")]
public async Task<IHttpActionResult> Retrieve()
{
var x = User;
x는
System.Security.Claims.ClaimsPrinciple and
System.Security.Claims.ClaimsIdentity
의 값을 가져옵니다하지만 X에서 내가 주장 자체에 대한 정보를 찾을 수 없습니다 : 나는이 방법으로 다음을 수행 할 수 있습니다 알고 있습니다. 내가 제안 이전에 SO 게시하려고했다
참고 :
var identity = (ClaimsIdentity)User.Identity;
IEnumerable<Claim> claims = identity.Claims;
// The following line returns null
var roles = identity.Claims.Where(r => r.Type == "roles").FirstOrDefault();
하지만 여전히 주장의 내부 내가 역할 주장과 관련 한 정보를 찾을 수 없습니다. 나는 그것이 클라이언트에게 도착할 때 거기에 있어야한다는 것을 안다.
"역할"클레임을 구체적으로 찾고 있습니다. 시스템에서 생성 된 클레임이 아닙니다. 그러나 내가 자신을 추가하려고 시도한 것은 역할의 연결 목록을 포함하고 있습니다.
이 나를 위해 작동하지 않습니다를 추가했다. 나는 ClaimTypes.Role으로 추가 된 것을 추가하지 않고 자신의 소유권 주장의 내용을 찾고 싶습니다. 감사. –
귀하의 조언에 감사드립니다. 나는 거기에 더 많은 예제가 있었으면 좋겠다. 나는 다른 웹 링크를 보면서 많은 시간을 보냈지 만 최근에는 아무것도 찾지 못했고 기술이 너무 빨리 변하기 때문에 계속 유지하기가 어렵다. –