|
Apologies for the shouting but this is important.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid..
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
For those new to message boards please try to follow a few simple rules when posting your question.- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode "<" (and other HTML) characters when pasting" checkbox before pasting anything inside the PRE block, and make sure "Use HTML in this post" check box is checked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question into an unrelated forum such as the lounge. It will be deleted. Likewise, do not post the same question in more than one forum.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
Hello,
I am new using a token, but i have to use it in a little WCF.
I found this code here.
As i can see, this generate a token, but i have a few questions:
1. What is Payload and it is each variable?
2. After generate de Token, how to i use in my WCF?
3. Do i have to put the CODE in the WCF?
CODE
static void Main(string[] args)
{
Console.WriteLine("");
string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b372742
9090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
var securityKey = new Microsoft
.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials
(securityKey, SecurityAlgorithms.HmacSha256Signature);
var header = new JwtHeader(credentials);
var payload = new JwtPayload
{
{ "some ", "hello "},
{ "scope", "http://dummy.com/"},
};
var secToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();
var tokenString = handler.WriteToken(secToken);
Console.WriteLine(tokenString);
Console.WriteLine("Consume Token");
var token = handler.ReadJwtToken(tokenString);
Console.WriteLine(token.Payload.First().Value);
Console.ReadLine();
}
|
|
|
|
|
I am getting this error when I try to compile the below code:
string key = "mykey";
byte[] buf = Decrypt(key, payload);
unsafe
{
fixed(byte* ptr = buf)
{
IntPtr memoryAddress = (IntPtr)ptr;
VirtualProtect(memoryAddress, (UIntPtr)buf.Length, (UInt32)Protection.PAGE_EXECUTE_READWRITE, out uint lpfOldProtect);
ShellcodeDelegate func = (ShellcodeDelegate)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(ShellcodeDelegate));
func();
}
Errors:
pi.cs(60,124): error CS1525: Invalid expression term 'uint'
pi.cs(60,138): error CS1002: ; expected
pi.cs(60,138): error CS1525: Invalid expression term ')'
ooooo
modified yesterday.
|
|
|
|
|
Check your C# version - the syntax Foo(out type varName) was only added at C#7.0
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks for your answer. It was a version issue.
ooooo
|
|
|
|
|
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Complex class from Numerics does not have any Parse method
How to write parse method
public static bool TryParse(string s,out Complex z)
{
}
|
|
|
|
|
And?
What have you tried?
Where are you stuck?
What help do you need?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Member 15634071 wrote: in CE (Central Europe) where I live it is comma Yes, that makes it 'interesting' if parsing the (a,b) format as you have no idea whether the ',' is a dec point or a value separator. e.g. (1,2,3,4) could only be 1.2+3.4i but (1,2,3) could be 1.2+3i or 1+2.3i Just to make it even more fun, US / UK / other countries have numbers where a comma can be a thousands separator e.g. (234.567,999,888.000) could be one of several complex numbers. e.g. 234.567+999888.0i, 234.567999+888.0i, or 234.567999888+0i.
Have you looked at how Point objects are parsed (they too can have an (x,y) format where x and y are decimal numbers)?
|
|
|
|
|
Can you write en example ?
Normally not in the comment section
|
|
|
|
|
If you are not familiar with this technique, I will publish a Tip/Trick soon.
Assuming you know how a nested inner class (which implements an Interface) can be declared 'private, and still be accessed by public methods in the enclosing outer class: which does make direct access to inner classes of the OuterClass prohibited.
Do you think the technique shown here is evil, undesirable, or ... ?
usage:
OuterClass1 oc1 = new OuterClass1();
var cat1 = oc1.AddSomething(new Cat("cat1", "meow"));
var cat2 = oc1.AddSomething(new Cat("cat2", "hiss"));
var dog1 = oc1.AddSomething(new Dog("dog1"));
var cat1instance = oc1.GetSomething("cat1"); class:
using System;
using System.Collections.Generic;
using System.Linq;
namespace NestedClassExample1
{
public interface ISomething1
{
string Name { get; }
}
public class OuterClass1
{
private List<ISomething1> Somethings = new List<ISomething1>();
internal ISomething1 AddSomething(ISomething1 newsomething)
{
Somethings.Add(newsomething);
return newsomething;
}
internal ISomething1 GetSomething(string name)
{
return Somethings.FirstOrDefault(smtng => smtng.Name == name);
}
}
public class Cat : ISomething1
{
public Cat(string name, string sound = "")
{
Name = name;
CatSound = sound;
}
public string Name { get; }
public string CatSound { set; get; }
}
public class Dog : ISomething1
{
public Dog(string name, string sound = "")
{
Name = name;
DogSound = sound;
}
public string Name { get; }
public string DogSound { set; get; }
}
public class Tiger : ISomething1
{
public Tiger(string name) { Name = name; }
public string Name { get; }
}
}
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
modified 4 days ago.
|
|
|
|
|
Either I'm asleep, or I'm missing something - I can't see anything accessing private members outside nested classes ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi Griff, I bet my brain-fog right now is thicker than yours
Of course, you are right that inner nested classes declared 'private are inaccessible in all cases to users of instances of the Outer class.
In this case, it is the fact the inner class 'Something1 (declared 'private) inherits from a public interface that allows the 'AddSomething method in the outer class to both update the private list, and return the new 'ISomething1.
Any other access declaration on the 'Something1 class would mean it is exposed to users of the outer class.
imho, this is part of the weirdness with which nested classes are handled in C#.
And, in my head, now, I see using this method is a way ... awkward ! ... to achieve strong encapsulation, as taught by Saint SOLID.
I eagerly await enlightenment !
cheers, Bill
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Being pedantic, the class implements the interface; it doen't inherit from it.
And there's no actual reference to the nested Something1 class anywhere in that code. You could remove that class, and the code would continue to run and function as expected.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
"pedantic" ? oh, we know what to do with that
Richard Deeming wrote: And there's no actual reference to the nested Something1 class anywhere in that code. That's the point: the consumer of an instance of 'OuterClass has no direct access to the plumbing ... except through those publicly exposed methods.
Richard Deeming wrote: You could remove that class, and the code would continue to run and function as expected. Wrong.
See Harold's response and my reply below.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
modified 5 days ago.
|
|
|
|
|
|
Richard Deeming wrote: The AddSomething and GetSomething methods depend on the interface, not the unused nested class. Hi, Thanks for looking further into the code !
You are correct that the class definition for 'ISomething1 is not used in the use-case example (it's a fossil from an earlier version), and I will delete that from the code posted here, thanks ! Sorry for any confusion that added.
Those two methods, by design, can only be accessed via an instance of 'OuterClass; while they use the public Interface, I don't see them as "dependent" in the usual sense of that term.
My hypothesis here is limited in scope: does this technique contribute to the encapsulation of the collections of Instances in the outer class that implement the Interface.
This code example is, by design, a terse example.
I also use generics in a way not shown in this example ... imho, does not need to be shown.
The "big picture," imho, is:
1) if you are managing collections of mixed Types (which requires use of Interface), where extensibility ... the user can create new Types that use the Interface ... is important ...
2) and, you want maximum encapsulation of those collections
3) what are good techniques ?
cheers, Bill
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
modified 4 days ago.
|
|
|
|
|
I'm not sure I follow. Are you saying you want to prevent the user from creating new types that implement the interface outside of your assembly?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: prevent the user from creating new types that implement the interface outside of your assembly? That is a very logical next step, but this example is limited to only preventing use of the OuterClass' collections,
In this example, the end user is free to create as many Dogs and Cats as they can afford
p.s. the code gets more interesting when you define an encapsulated instantiator like this:
internal T NewInstance<T>(params object[] args)
where T : class, ISomething1
{
var ttype = typeof(T);
ISomething1 instance = null;
instance = (T) Activator.CreateInstance(ttype, args);
Somethings.Add(instance);
return (T) instance;
} Which is, of course, only one way to handle instantiation; I tend to avoid 'Activator.CreateInstance for reasons I am sure you are aware of.
Returning to the "big picture:" what strategies do you use to increase the encapsulation of classes that maintain collections of mixed types ?
cheers, Bill
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
I'm still not entire sure what you're trying to achieve.
If a generic collection contains mixed types, those types must either inherit from a base class, or implement an interface. If the collection class is public, then trying to further restrict the types it can contain to those in the current assembly, or an approved list of assemblies, is usually a sign of a broken design.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks !Richard Deeming wrote: If the collection class is public, then trying to further restrict the type ... In this case, the 'OuterClass is public; the whole point of this (encapsulation) is allowing access to the internal collections inside that outer class only by methods I expose.
I think you are reading into this some broader issue.Richard Deeming wrote: ... to those in the current assembly, or an approved list of assemblies, is usually a sign of a broken design. I can easily imagine a scenario where a design of an outer class with private internal classes would be very useful. But, that is not relevant here.
cheers, Bill
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
I'd be more impressed if that code actually compiled. But even then I don't really understand what point you are trying to demonstrate.
|
|
|
|
|
Thanks, added the missing 'GetSomething method, and verified it compiles/runs as expected..
Re "point:" please see response to Griff; if there is anyone on the planet I think would instantly see the "point," and be aware of the issue ... and blow my mind with a better technique ...I would pick: you
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
If the trick is intended to center around being able to return instances of a private class (which otherwise results in the dreaded "inconsistent accessibility" error) in an at least someone useful way (you can always return it as object, but then what?), then that's known .. by me at least, but I guess in general. The code doesn't really show that in action though. I wouldn't rate that an evil trick, more like "limited usefulness".
Otherwise if it's supposed to be a different trick then I didn't get it.
|
|
|
|
|