IdentityServer / IdentityServer2

[deprecated] Thinktecture IdentityServer is a light-weight security token service built with .NET 4.5, MVC 4, Web API and WCF.
Other
410 stars 291 forks source link

"Authorization for token issuance failed because the user is anonymous" when calling service from console client. #887

Open ShubhraBakshi opened 7 years ago

ShubhraBakshi commented 7 years ago

I'm trying to access the service from console client, which is protected by Identity Server2. Every time I'm getting the error: Authorization for token issuance failed because the user is anonymous from thinktectureIdentityServer trace log and not getting into the service. From the browser I'm able to login into the service. I'll paste my sample code here:

Service web config:

The Client: namespace Client { class Program { static string _idsrvEndpoint = "https://localhost/FedProvider/issue/wstrust/mixed/username"; static string _realm = "https://localhost:44350/"; static void Main(string[] args) { var token = RequestToken(); CallService(token); } private static void CallService(SecurityToken token) { var serviceEndpoint = "https://" + "localhost:44350" + "/Service1.svc"; var binding = new WS2007FederationHttpBinding(WSFederationHttpSecurityMode.TransportWithMessageCredential); binding.Security.Message.EstablishSecurityContext = false; binding.Security.Message.IssuedKeyType = SecurityKeyType.BearerKey; var factory = new ChannelFactory(binding, new EndpointAddress(serviceEndpoint)); factory.Credentials.SupportInteractive = false; var channel = factory.CreateChannelWithIssuedToken(token); var claims = channel.TestService(); // claims.ForEach(c => Console.WriteLine("{0}\n {1}\n\n", c.Type, c.Value)); } private static SecurityToken RequestToken() { try { var binding = new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential); var credentials = new ClientCredentials(); credentials.UserName.UserName = "test"; credentials.UserName.Password = "test123"; System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => { return true; }; return WSTrustClient.Issue( new EndpointAddress(_idsrvEndpoint), new EndpointAddress(_realm), binding, credentials); } catch (FaultException ex ) { } catch(Exception) { } return null; } } Any help is grateful, as it has already taken my lot of time.