Click here to Skip to main content
15,888,579 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# Specified cast is not valid Pin
OriginalGriff22-Dec-16 22:11
mveOriginalGriff22-Dec-16 22:11 
AnswerRe: C# Specified cast is not valid Pin
Nelson Costa Inácio22-Dec-16 22:42
Nelson Costa Inácio22-Dec-16 22:42 
GeneralRe: C# Specified cast is not valid Pin
harold aptroot22-Dec-16 22:45
harold aptroot22-Dec-16 22:45 
GeneralRe: C# Specified cast is not valid Pin
OriginalGriff23-Dec-16 0:05
mveOriginalGriff23-Dec-16 0:05 
AnswerRe: C# Specified cast is not valid Pin
Pete O'Hanlon23-Dec-16 0:00
mvePete O'Hanlon23-Dec-16 0:00 
AnswerRe: C# Specified cast is not valid Pin
Gerry Schmitz23-Dec-16 6:50
mveGerry Schmitz23-Dec-16 6:50 
GeneralRe: C# Specified cast is not valid Pin
Richard MacCutchan23-Dec-16 6:59
mveRichard MacCutchan23-Dec-16 6:59 
QuestionWhat is this JWT Code Doing? Pin
TheOnlyRealTodd21-Dec-16 15:46
professionalTheOnlyRealTodd21-Dec-16 15:46 
I'm following a tutorial for Web API Jwt Tokens but am confused on what exactly this below code is doing. When I read tutorials, I like to take the time to understand the content rather than just sorta copy/paste blow through them. If anyone is familiar with this type of code, please give me a little walkthrough. I'll go ahead and narrate how I feel this is working below to start it off:

C#
public string Protect(AuthenticationTicket data)
 public class CustomJwtFormat : ISecureDataFormat<AuthenticationTicket>
    {
    
        private readonly string _issuer = string.Empty;
 
        public CustomJwtFormat(string issuer)
        {
            _issuer = issuer;
        }
 
        public string Protect(AuthenticationTicket data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
 
            string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];
 
            string symmetricKeyAsBase64 = ConfigurationManager.AppSettings["as:AudienceSecret"];
 
            var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64);
 
            var signingKey = new HmacSigningCredentials(keyByteArray);
 
            var issued = data.Properties.IssuedUtc;
            
            var expires = data.Properties.ExpiresUtc;
 
            var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);
 
            var handler = new JwtSecurityTokenHandler();
 
            var jwt = handler.WriteToken(token);
 
            return jwt;
        }



The code appears to:

1. Accept user claims as an argument called "data" (?)

2. After ensuring the data isn't null, it pulls an "AudienceId" and "AudienceSecret" from AppSettings in web.config and assigns to two variables.

3. It decodes the AudienceSecret from Base64Url into a byte array? <----This is where I'm confused. The secret is just a URL???

4. It now takes the decoded URL and then passes it into a hash function, creating a keyed-hash message authentication code "signing credentials" (also confused a bit here)

5. Assigns issued and expiry date to the claims/data.

6. It then creates a token with the above data

7.News up a "token handler" and then creates yet another jwt token variable and finally apparently receives the token from handler.WriteToken(token)


This is my best guess but there are so many types thrown around here, this code is actually quite difficult to understand, especially coming back to OOP from C; it seems very convoluted with big words and not a ton of meaning.

Any additional info or tips would be appreciated, especially about the "audience." I don't get the relationship between the URL/Base64encoded/string/key thing Thank you!

modified 21-Dec-16 22:10pm.

GeneralRe: What is this JWT Code Doing? Pin
Richard MacCutchan21-Dec-16 21:54
mveRichard MacCutchan21-Dec-16 21:54 
GeneralRe: What is this JWT Code Doing? Pin
TheOnlyRealTodd21-Dec-16 22:35
professionalTheOnlyRealTodd21-Dec-16 22:35 
GeneralRe: What is this JWT Code Doing? Pin
OriginalGriff21-Dec-16 23:24
mveOriginalGriff21-Dec-16 23:24 
GeneralRe: What is this JWT Code Doing? Pin
Gerry Schmitz22-Dec-16 4:07
mveGerry Schmitz22-Dec-16 4:07 
AnswerRe: What is this JWT Code Doing? Pin
Richard Deeming22-Dec-16 2:35
mveRichard Deeming22-Dec-16 2:35 
QuestionNeed help on how to if record exists update else insert c# Pin
Bootzilla3321-Dec-16 7:56
Bootzilla3321-Dec-16 7:56 
AnswerRe: Need help on how to if record exists update else insert c# Pin
Michael_Davies21-Dec-16 8:04
Michael_Davies21-Dec-16 8:04 
AnswerRe: Need help on how to if record exists update else insert c# Pin
Richard Deeming21-Dec-16 8:06
mveRichard Deeming21-Dec-16 8:06 
AnswerRe: Need help on how to if record exists update else insert c# Pin
Nelson Costa Inácio21-Dec-16 23:34
Nelson Costa Inácio21-Dec-16 23:34 
QuestionC# PictureBox remove image Pin
Pavlex420-Dec-16 11:21
Pavlex420-Dec-16 11:21 
AnswerRe: C# PictureBox remove image Pin
Pavlex420-Dec-16 11:39
Pavlex420-Dec-16 11:39 
GeneralRe: C# PictureBox remove image Pin
Mohan Baro20-Dec-16 18:10
Mohan Baro20-Dec-16 18:10 
GeneralRe: C# PictureBox remove image Pin
Pavlex420-Dec-16 20:11
Pavlex420-Dec-16 20:11 
SuggestionRe: C# PictureBox remove image Pin
Ralf Meier20-Dec-16 21:19
mveRalf Meier20-Dec-16 21:19 
GeneralRe: C# PictureBox remove image Pin
Pavlex420-Dec-16 21:24
Pavlex420-Dec-16 21:24 
GeneralRe: C# PictureBox remove image Pin
Ralf Meier21-Dec-16 1:18
mveRalf Meier21-Dec-16 1:18 
GeneralRe: C# PictureBox remove image Pin
Pavlex421-Dec-16 8:21
Pavlex421-Dec-16 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.