Kontekst: Używam programu ASP.NET MVC z własnym hostem OWIN. Poniżej znajdują się pozostałe konfiguracje/ustawienia.IdentityServer3: Niektóre roszczenia nie są zwracane z serwera tożsamości
W moich Client sw serwer tożsamości (zawiadomienie AllowedScopes zestawu):
public static class InMemoryClientSource
{
public static List<Client> GetClientList()
{
return new List<Client>()
{
new Client()
{
ClientName = "Admin website",
ClientId = "admin",
Enabled = true,
Flow = Flows.Hybrid,
ClientSecrets = new List<Secret>()
{
new Secret("admin".Sha256())
},
RedirectUris = new List<string>()
{
"https://admin.localhost.com/"
},
PostLogoutRedirectUris = new List<string>()
{
"https://admin.localhost.com/"
},
AllowedScopes = new List<string> {
Constants.StandardScopes.OpenId,
Constants.StandardScopes.Profile,
Constants.StandardScopes.Email,
Constants.StandardScopes.Roles
}
}
};
}
}
Oto celownicze:
public static class InMemoryScopeSource
{
public static List<Scope> GetScopeList()
{
var scopes = new List<Scope>();
scopes.Add(StandardScopes.OpenId);
scopes.Add(StandardScopes.Profile);
scopes.Add(StandardScopes.Email);
scopes.Add(StandardScopes.Roles);
return scopes.ToList();
}
}
W Identity Server, Oto konfiguracja serwera. (Zawiadomienie Klientom i zakresów są te podane powyżej):
var userService = new UsersService(.... repository passed here ....);
var factory = new IdentityServerServiceFactory()
.UseInMemoryClients(InMemoryClientSource.GetClientList())
.UseInMemoryScopes(InMemoryScopeSource.GetScopeList());
factory.UserService = new Registration<IUserService>(resolver => userService);
var options = new IdentityServerOptions()
{
Factory = factory,
SigningCertificate = Certificates.Load(), // certificates blah blah
SiteName = "Identity"
};
app.UseIdentityServer(options);
Wreszcie, po stronie aplikacji Web Client, to w jaki sposób uwierzytelniania jest skonfigurowana:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
{
Authority = "https://id.localhost.com",
ClientId = "admin",
RedirectUri = "https://admin.localhost.com/",
PostLogoutRedirectUri = "https://admin.localhost.com/",
ResponseType = "code id_token token",
Scope = "openid profile email roles",
ClientSecret = "admin",
SignInAsAuthenticationType = "Cookies"
});
mam zaimplementowano niestandardową klasę dla usługi IUserService:
public class UsersService : UserServiceBase
{
public UsersService(.... repository passed here ....)
{
//.... ctor stuff
}
public override Task AuthenticateLocalAsync(LocalAuthenticationContext context)
{
// var user = .... retrieved from database .....
// ... auth logic ...
if (isAuthenticated)
{
var claims = new List<Claim>();
claims.Add(new Claim(Constants.ClaimTypes.GivenName, user.FirstName));
claims.Add(new Claim(Constants.ClaimTypes.FamilyName, user.LastName));
claims.Add(new Claim(Constants.ClaimTypes.Email, user.EmailAddress));
context.AuthenticateResult = new AuthenticateResult(user.Id.ToString(), user.EmailAddress, claims);
}
return Task.FromResult(0);
}
}
Jak widzisz, roszczenia są przekazywane w tej linii:
context.AuthenticateResult = new AuthenticateResult(user.Id.ToString(), user.EmailAddress, claims);
Kiedy próbuję zalogować się do IdentityServer3, mogę zalogować się z powodzeniem do aplikacji internetowej klienta. JEDNAK, gdy otrzymam roszczenia użytkownika, nie widzę żadnych roszczeń tożsamości. Nie given_name, family_name, i email roszczenia. Zrzut ekranu poniżej:
Wszystko mógłbym przegapić? Z góry dziękuję!
Konfiguracja wygląda dobrze. Głupio wiem, ale czy jesteś pewien, że uwierzytelniony użytkownik ma te roszczenia? –
Hej @ScottBrady, przypuszczam, że już mają, ponieważ dodałem te roszczenia do kontekstu.AuthenticateResult (jak widać na przykładowym kodzie powyżej dla niestandardowej implementacji UsersService). – jerbersoft