ASP.NET Core Jwt implement signinmanager claims












0














I have implemented Jwt as a way to authenticate my user. However, I am stuck on how I can do certain things on my application with regards to roles. Currently my Jwt Token contains the users email, phone , id and a list of roles that they have.



What I do with that token is like this:



[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object {
ApplicationGlobals.ApplicationSecretKey, RoleGlobals.SystemAdministrator
})]
public IActionResult Index()
{
return View();
}


My Typefilter contains a rest request that sends the token to another application to verify if my user can access that Function. However,
I am stuck when it comes to the view. I want to segment certain containers to be allowed to be viewed by certain users with certain roles.



I have an idea that if I were to add my users claims to the signinmanager just like a non jwt application, i would be able to get the claims from the httpcontext. However, I don't know if what I have can work with an application that uses jwt.



public async Task SignInUserAsync(TIdentityUser user, bool isPersistent, IEnumerable<Claim> customClaims)
{
var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(user);
var identity = claimsPrincipal.Identity as ClaimsIdentity;
var claims = (from c in claimsPrincipal.Claims select c).ToList();
var savedClaims = claims;
foreach (var item in claims)
{
identity.RemoveClaim(item);
}
if (customClaims != null)
{
identity.AddClaim(savedClaims[0]);
identity.AddClaim(savedClaims[1]);
identity.AddClaim(savedClaims[2]);
identity.AddClaims(customClaims);
}
await _signInManager.Context.SignInAsync(IdentityConstants.ApplicationScheme,
claimsPrincipal,
new AuthenticationProperties { IsPersistent = isPersistent });
}









share|improve this question
























  • have you checked this out? stackoverflow.com/questions/18677837/…
    – Sujit.Warrier
    Nov 12 '18 at 6:40
















0














I have implemented Jwt as a way to authenticate my user. However, I am stuck on how I can do certain things on my application with regards to roles. Currently my Jwt Token contains the users email, phone , id and a list of roles that they have.



What I do with that token is like this:



[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object {
ApplicationGlobals.ApplicationSecretKey, RoleGlobals.SystemAdministrator
})]
public IActionResult Index()
{
return View();
}


My Typefilter contains a rest request that sends the token to another application to verify if my user can access that Function. However,
I am stuck when it comes to the view. I want to segment certain containers to be allowed to be viewed by certain users with certain roles.



I have an idea that if I were to add my users claims to the signinmanager just like a non jwt application, i would be able to get the claims from the httpcontext. However, I don't know if what I have can work with an application that uses jwt.



public async Task SignInUserAsync(TIdentityUser user, bool isPersistent, IEnumerable<Claim> customClaims)
{
var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(user);
var identity = claimsPrincipal.Identity as ClaimsIdentity;
var claims = (from c in claimsPrincipal.Claims select c).ToList();
var savedClaims = claims;
foreach (var item in claims)
{
identity.RemoveClaim(item);
}
if (customClaims != null)
{
identity.AddClaim(savedClaims[0]);
identity.AddClaim(savedClaims[1]);
identity.AddClaim(savedClaims[2]);
identity.AddClaims(customClaims);
}
await _signInManager.Context.SignInAsync(IdentityConstants.ApplicationScheme,
claimsPrincipal,
new AuthenticationProperties { IsPersistent = isPersistent });
}









share|improve this question
























  • have you checked this out? stackoverflow.com/questions/18677837/…
    – Sujit.Warrier
    Nov 12 '18 at 6:40














0












0








0







I have implemented Jwt as a way to authenticate my user. However, I am stuck on how I can do certain things on my application with regards to roles. Currently my Jwt Token contains the users email, phone , id and a list of roles that they have.



What I do with that token is like this:



[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object {
ApplicationGlobals.ApplicationSecretKey, RoleGlobals.SystemAdministrator
})]
public IActionResult Index()
{
return View();
}


My Typefilter contains a rest request that sends the token to another application to verify if my user can access that Function. However,
I am stuck when it comes to the view. I want to segment certain containers to be allowed to be viewed by certain users with certain roles.



I have an idea that if I were to add my users claims to the signinmanager just like a non jwt application, i would be able to get the claims from the httpcontext. However, I don't know if what I have can work with an application that uses jwt.



public async Task SignInUserAsync(TIdentityUser user, bool isPersistent, IEnumerable<Claim> customClaims)
{
var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(user);
var identity = claimsPrincipal.Identity as ClaimsIdentity;
var claims = (from c in claimsPrincipal.Claims select c).ToList();
var savedClaims = claims;
foreach (var item in claims)
{
identity.RemoveClaim(item);
}
if (customClaims != null)
{
identity.AddClaim(savedClaims[0]);
identity.AddClaim(savedClaims[1]);
identity.AddClaim(savedClaims[2]);
identity.AddClaims(customClaims);
}
await _signInManager.Context.SignInAsync(IdentityConstants.ApplicationScheme,
claimsPrincipal,
new AuthenticationProperties { IsPersistent = isPersistent });
}









share|improve this question















I have implemented Jwt as a way to authenticate my user. However, I am stuck on how I can do certain things on my application with regards to roles. Currently my Jwt Token contains the users email, phone , id and a list of roles that they have.



What I do with that token is like this:



[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object {
ApplicationGlobals.ApplicationSecretKey, RoleGlobals.SystemAdministrator
})]
public IActionResult Index()
{
return View();
}


My Typefilter contains a rest request that sends the token to another application to verify if my user can access that Function. However,
I am stuck when it comes to the view. I want to segment certain containers to be allowed to be viewed by certain users with certain roles.



I have an idea that if I were to add my users claims to the signinmanager just like a non jwt application, i would be able to get the claims from the httpcontext. However, I don't know if what I have can work with an application that uses jwt.



public async Task SignInUserAsync(TIdentityUser user, bool isPersistent, IEnumerable<Claim> customClaims)
{
var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(user);
var identity = claimsPrincipal.Identity as ClaimsIdentity;
var claims = (from c in claimsPrincipal.Claims select c).ToList();
var savedClaims = claims;
foreach (var item in claims)
{
identity.RemoveClaim(item);
}
if (customClaims != null)
{
identity.AddClaim(savedClaims[0]);
identity.AddClaim(savedClaims[1]);
identity.AddClaim(savedClaims[2]);
identity.AddClaims(customClaims);
}
await _signInManager.Context.SignInAsync(IdentityConstants.ApplicationScheme,
claimsPrincipal,
new AuthenticationProperties { IsPersistent = isPersistent });
}






asp.net asp.net-core asp.net-core-mvc jwt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 5:41









Tetsuya Yamamoto

14.5k41939




14.5k41939










asked Nov 12 '18 at 5:38









JianYA

4861024




4861024












  • have you checked this out? stackoverflow.com/questions/18677837/…
    – Sujit.Warrier
    Nov 12 '18 at 6:40


















  • have you checked this out? stackoverflow.com/questions/18677837/…
    – Sujit.Warrier
    Nov 12 '18 at 6:40
















have you checked this out? stackoverflow.com/questions/18677837/…
– Sujit.Warrier
Nov 12 '18 at 6:40




have you checked this out? stackoverflow.com/questions/18677837/…
– Sujit.Warrier
Nov 12 '18 at 6:40












2 Answers
2






active

oldest

votes


















0














JSON Web Tokens consist of three parts separated by dots (.), which are: Header,Payload,Signature .Therefore, a JWT typically looks like xxxxx.yyyyy.zzzzz .The second part of the token is the payload, which contains the claims.



You can decode the access token to get the claims which related to your roles :



How to decode JWT Token? .



Decoding and verifying JWT token using System.IdentityModel.Tokens.Jwt



If you are using Owin OpenID Connect middlerware to autheticate user from identity provider like Azure AD , Idenity server 4.... You can add additional claims to principal under OnTokenValidated event .



Edit :



You can also add the claims(decode and get the claims) to user context before sign- in :



 var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, loginData.Username));
identity.AddClaim(new Claim(ClaimTypes.Name, loginData.Username));
//add your custom claims
....

var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = loginData.RememberMe });


Reference : http://future-shock.net/blog/post/creating-a-simple-login-in-asp.net-core-2-using-authentication-and-authorization-not-identity



Then you can access the claims in view like :



@foreach (var item in Context.User.Claims)
{
<p>@item.Value</p>
};





share|improve this answer























  • Once I decode the token on the client side how can I store it in the httpcontext to pass it to the view?
    – JianYA
    Nov 13 '18 at 8:53










  • I’d prefer to not use cookies if possible
    – JianYA
    Nov 13 '18 at 8:53










  • @JianYA ,just store the role claim to viewbag , add show/hide the content base on that value in view .
    – Nan Yu
    Nov 13 '18 at 8:55










  • But doesn't that mean if I store the role claim to a viewbag, if i have something like a navigation menu that has links for a certain role, won't i have to add it for the entire application?
    – JianYA
    Nov 13 '18 at 11:39










  • @JianYA ,see edited reply , you could store in user claim or server side storage.
    – Nan Yu
    Nov 14 '18 at 5:13



















1














I am recently doing a cooperative project on JWT. I wrote a middlware, when ever the user request to the api, It is checked by the Authentication middleware. I read the userRole from db and put it in the identity priciple I am sharing the middleware codes.



In here I read the JWT middle part to extract the user information



public class AuthenticationMiddleware
{
private readonly RequestDelegate _next;

// Dependency Injection
public AuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
string authHeader = context.Request.Headers["Authorization"];

if (authHeader != null)
{
int startPoint = authHeader.IndexOf(".") + 1;
int endPoint = authHeader.LastIndexOf(".");

var tokenString = authHeader.Substring(startPoint, endPoint - startPoint).Split(".");
var token = tokenString[0].ToString()+"==";

var credentialString = Encoding.UTF8
.GetString(Convert.FromBase64String(token));

// Splitting the data from Jwt
var credentials = credentialString.Split(new char { ':',',' });

// Trim this string.
var userRule = credentials[5].Replace(""", "");
var userName = credentials[3].Replace(""", "");

// Identity Principal
var claims = new
{
new Claim("name", userName),
new Claim(ClaimTypes.Role, userRule),

};
var identity = new ClaimsIdentity(claims, "basic");
context.User = new ClaimsPrincipal(identity);
}
await _next(context);
}


}


In startup.cs you need to call this middleware in the configure method



 app.UseMiddleware<AuthenticationMiddleware>();


In the controller



 [HttpGet("GetUsers")]
[Authorize(Roles = "admin")]
public ActionResult GetUsers()
{
var users = _authRepository.GetUsers();
return Ok(users);
}


if You need any help please give a comment. This implementation really worked for me. Check my repositories on the subject: https://github.com/hidayatarg/Asp.net-Core-2.1-Jwt-Authentication-Middleware
https://github.com/hidayatarg/Decode-JWT-Token






share|improve this answer























  • Hello, thank you for answering. I tried that method but then I needed a more customised function thats why I used the typefilter instead.
    – JianYA
    Nov 12 '18 at 22:14










  • Since Jwt sign the headers of our request. Here i am reading the header specifically the middle part jwt which is called the JWT pay load if you want to see the full exampl echeck my repo github.com/hidayatarg/…
    – Hidayat Arghandabi
    Nov 13 '18 at 7:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256440%2fasp-net-core-jwt-implement-signinmanager-claims%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














JSON Web Tokens consist of three parts separated by dots (.), which are: Header,Payload,Signature .Therefore, a JWT typically looks like xxxxx.yyyyy.zzzzz .The second part of the token is the payload, which contains the claims.



You can decode the access token to get the claims which related to your roles :



How to decode JWT Token? .



Decoding and verifying JWT token using System.IdentityModel.Tokens.Jwt



If you are using Owin OpenID Connect middlerware to autheticate user from identity provider like Azure AD , Idenity server 4.... You can add additional claims to principal under OnTokenValidated event .



Edit :



You can also add the claims(decode and get the claims) to user context before sign- in :



 var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, loginData.Username));
identity.AddClaim(new Claim(ClaimTypes.Name, loginData.Username));
//add your custom claims
....

var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = loginData.RememberMe });


Reference : http://future-shock.net/blog/post/creating-a-simple-login-in-asp.net-core-2-using-authentication-and-authorization-not-identity



Then you can access the claims in view like :



@foreach (var item in Context.User.Claims)
{
<p>@item.Value</p>
};





share|improve this answer























  • Once I decode the token on the client side how can I store it in the httpcontext to pass it to the view?
    – JianYA
    Nov 13 '18 at 8:53










  • I’d prefer to not use cookies if possible
    – JianYA
    Nov 13 '18 at 8:53










  • @JianYA ,just store the role claim to viewbag , add show/hide the content base on that value in view .
    – Nan Yu
    Nov 13 '18 at 8:55










  • But doesn't that mean if I store the role claim to a viewbag, if i have something like a navigation menu that has links for a certain role, won't i have to add it for the entire application?
    – JianYA
    Nov 13 '18 at 11:39










  • @JianYA ,see edited reply , you could store in user claim or server side storage.
    – Nan Yu
    Nov 14 '18 at 5:13
















0














JSON Web Tokens consist of three parts separated by dots (.), which are: Header,Payload,Signature .Therefore, a JWT typically looks like xxxxx.yyyyy.zzzzz .The second part of the token is the payload, which contains the claims.



You can decode the access token to get the claims which related to your roles :



How to decode JWT Token? .



Decoding and verifying JWT token using System.IdentityModel.Tokens.Jwt



If you are using Owin OpenID Connect middlerware to autheticate user from identity provider like Azure AD , Idenity server 4.... You can add additional claims to principal under OnTokenValidated event .



Edit :



You can also add the claims(decode and get the claims) to user context before sign- in :



 var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, loginData.Username));
identity.AddClaim(new Claim(ClaimTypes.Name, loginData.Username));
//add your custom claims
....

var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = loginData.RememberMe });


Reference : http://future-shock.net/blog/post/creating-a-simple-login-in-asp.net-core-2-using-authentication-and-authorization-not-identity



Then you can access the claims in view like :



@foreach (var item in Context.User.Claims)
{
<p>@item.Value</p>
};





share|improve this answer























  • Once I decode the token on the client side how can I store it in the httpcontext to pass it to the view?
    – JianYA
    Nov 13 '18 at 8:53










  • I’d prefer to not use cookies if possible
    – JianYA
    Nov 13 '18 at 8:53










  • @JianYA ,just store the role claim to viewbag , add show/hide the content base on that value in view .
    – Nan Yu
    Nov 13 '18 at 8:55










  • But doesn't that mean if I store the role claim to a viewbag, if i have something like a navigation menu that has links for a certain role, won't i have to add it for the entire application?
    – JianYA
    Nov 13 '18 at 11:39










  • @JianYA ,see edited reply , you could store in user claim or server side storage.
    – Nan Yu
    Nov 14 '18 at 5:13














0












0








0






JSON Web Tokens consist of three parts separated by dots (.), which are: Header,Payload,Signature .Therefore, a JWT typically looks like xxxxx.yyyyy.zzzzz .The second part of the token is the payload, which contains the claims.



You can decode the access token to get the claims which related to your roles :



How to decode JWT Token? .



Decoding and verifying JWT token using System.IdentityModel.Tokens.Jwt



If you are using Owin OpenID Connect middlerware to autheticate user from identity provider like Azure AD , Idenity server 4.... You can add additional claims to principal under OnTokenValidated event .



Edit :



You can also add the claims(decode and get the claims) to user context before sign- in :



 var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, loginData.Username));
identity.AddClaim(new Claim(ClaimTypes.Name, loginData.Username));
//add your custom claims
....

var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = loginData.RememberMe });


Reference : http://future-shock.net/blog/post/creating-a-simple-login-in-asp.net-core-2-using-authentication-and-authorization-not-identity



Then you can access the claims in view like :



@foreach (var item in Context.User.Claims)
{
<p>@item.Value</p>
};





share|improve this answer














JSON Web Tokens consist of three parts separated by dots (.), which are: Header,Payload,Signature .Therefore, a JWT typically looks like xxxxx.yyyyy.zzzzz .The second part of the token is the payload, which contains the claims.



You can decode the access token to get the claims which related to your roles :



How to decode JWT Token? .



Decoding and verifying JWT token using System.IdentityModel.Tokens.Jwt



If you are using Owin OpenID Connect middlerware to autheticate user from identity provider like Azure AD , Idenity server 4.... You can add additional claims to principal under OnTokenValidated event .



Edit :



You can also add the claims(decode and get the claims) to user context before sign- in :



 var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, loginData.Username));
identity.AddClaim(new Claim(ClaimTypes.Name, loginData.Username));
//add your custom claims
....

var principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = loginData.RememberMe });


Reference : http://future-shock.net/blog/post/creating-a-simple-login-in-asp.net-core-2-using-authentication-and-authorization-not-identity



Then you can access the claims in view like :



@foreach (var item in Context.User.Claims)
{
<p>@item.Value</p>
};






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 5:11

























answered Nov 13 '18 at 8:33









Nan Yu

6,2202652




6,2202652












  • Once I decode the token on the client side how can I store it in the httpcontext to pass it to the view?
    – JianYA
    Nov 13 '18 at 8:53










  • I’d prefer to not use cookies if possible
    – JianYA
    Nov 13 '18 at 8:53










  • @JianYA ,just store the role claim to viewbag , add show/hide the content base on that value in view .
    – Nan Yu
    Nov 13 '18 at 8:55










  • But doesn't that mean if I store the role claim to a viewbag, if i have something like a navigation menu that has links for a certain role, won't i have to add it for the entire application?
    – JianYA
    Nov 13 '18 at 11:39










  • @JianYA ,see edited reply , you could store in user claim or server side storage.
    – Nan Yu
    Nov 14 '18 at 5:13


















  • Once I decode the token on the client side how can I store it in the httpcontext to pass it to the view?
    – JianYA
    Nov 13 '18 at 8:53










  • I’d prefer to not use cookies if possible
    – JianYA
    Nov 13 '18 at 8:53










  • @JianYA ,just store the role claim to viewbag , add show/hide the content base on that value in view .
    – Nan Yu
    Nov 13 '18 at 8:55










  • But doesn't that mean if I store the role claim to a viewbag, if i have something like a navigation menu that has links for a certain role, won't i have to add it for the entire application?
    – JianYA
    Nov 13 '18 at 11:39










  • @JianYA ,see edited reply , you could store in user claim or server side storage.
    – Nan Yu
    Nov 14 '18 at 5:13
















Once I decode the token on the client side how can I store it in the httpcontext to pass it to the view?
– JianYA
Nov 13 '18 at 8:53




Once I decode the token on the client side how can I store it in the httpcontext to pass it to the view?
– JianYA
Nov 13 '18 at 8:53












I’d prefer to not use cookies if possible
– JianYA
Nov 13 '18 at 8:53




I’d prefer to not use cookies if possible
– JianYA
Nov 13 '18 at 8:53












@JianYA ,just store the role claim to viewbag , add show/hide the content base on that value in view .
– Nan Yu
Nov 13 '18 at 8:55




@JianYA ,just store the role claim to viewbag , add show/hide the content base on that value in view .
– Nan Yu
Nov 13 '18 at 8:55












But doesn't that mean if I store the role claim to a viewbag, if i have something like a navigation menu that has links for a certain role, won't i have to add it for the entire application?
– JianYA
Nov 13 '18 at 11:39




But doesn't that mean if I store the role claim to a viewbag, if i have something like a navigation menu that has links for a certain role, won't i have to add it for the entire application?
– JianYA
Nov 13 '18 at 11:39












@JianYA ,see edited reply , you could store in user claim or server side storage.
– Nan Yu
Nov 14 '18 at 5:13




@JianYA ,see edited reply , you could store in user claim or server side storage.
– Nan Yu
Nov 14 '18 at 5:13













1














I am recently doing a cooperative project on JWT. I wrote a middlware, when ever the user request to the api, It is checked by the Authentication middleware. I read the userRole from db and put it in the identity priciple I am sharing the middleware codes.



In here I read the JWT middle part to extract the user information



public class AuthenticationMiddleware
{
private readonly RequestDelegate _next;

// Dependency Injection
public AuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
string authHeader = context.Request.Headers["Authorization"];

if (authHeader != null)
{
int startPoint = authHeader.IndexOf(".") + 1;
int endPoint = authHeader.LastIndexOf(".");

var tokenString = authHeader.Substring(startPoint, endPoint - startPoint).Split(".");
var token = tokenString[0].ToString()+"==";

var credentialString = Encoding.UTF8
.GetString(Convert.FromBase64String(token));

// Splitting the data from Jwt
var credentials = credentialString.Split(new char { ':',',' });

// Trim this string.
var userRule = credentials[5].Replace(""", "");
var userName = credentials[3].Replace(""", "");

// Identity Principal
var claims = new
{
new Claim("name", userName),
new Claim(ClaimTypes.Role, userRule),

};
var identity = new ClaimsIdentity(claims, "basic");
context.User = new ClaimsPrincipal(identity);
}
await _next(context);
}


}


In startup.cs you need to call this middleware in the configure method



 app.UseMiddleware<AuthenticationMiddleware>();


In the controller



 [HttpGet("GetUsers")]
[Authorize(Roles = "admin")]
public ActionResult GetUsers()
{
var users = _authRepository.GetUsers();
return Ok(users);
}


if You need any help please give a comment. This implementation really worked for me. Check my repositories on the subject: https://github.com/hidayatarg/Asp.net-Core-2.1-Jwt-Authentication-Middleware
https://github.com/hidayatarg/Decode-JWT-Token






share|improve this answer























  • Hello, thank you for answering. I tried that method but then I needed a more customised function thats why I used the typefilter instead.
    – JianYA
    Nov 12 '18 at 22:14










  • Since Jwt sign the headers of our request. Here i am reading the header specifically the middle part jwt which is called the JWT pay load if you want to see the full exampl echeck my repo github.com/hidayatarg/…
    – Hidayat Arghandabi
    Nov 13 '18 at 7:36
















1














I am recently doing a cooperative project on JWT. I wrote a middlware, when ever the user request to the api, It is checked by the Authentication middleware. I read the userRole from db and put it in the identity priciple I am sharing the middleware codes.



In here I read the JWT middle part to extract the user information



public class AuthenticationMiddleware
{
private readonly RequestDelegate _next;

// Dependency Injection
public AuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
string authHeader = context.Request.Headers["Authorization"];

if (authHeader != null)
{
int startPoint = authHeader.IndexOf(".") + 1;
int endPoint = authHeader.LastIndexOf(".");

var tokenString = authHeader.Substring(startPoint, endPoint - startPoint).Split(".");
var token = tokenString[0].ToString()+"==";

var credentialString = Encoding.UTF8
.GetString(Convert.FromBase64String(token));

// Splitting the data from Jwt
var credentials = credentialString.Split(new char { ':',',' });

// Trim this string.
var userRule = credentials[5].Replace(""", "");
var userName = credentials[3].Replace(""", "");

// Identity Principal
var claims = new
{
new Claim("name", userName),
new Claim(ClaimTypes.Role, userRule),

};
var identity = new ClaimsIdentity(claims, "basic");
context.User = new ClaimsPrincipal(identity);
}
await _next(context);
}


}


In startup.cs you need to call this middleware in the configure method



 app.UseMiddleware<AuthenticationMiddleware>();


In the controller



 [HttpGet("GetUsers")]
[Authorize(Roles = "admin")]
public ActionResult GetUsers()
{
var users = _authRepository.GetUsers();
return Ok(users);
}


if You need any help please give a comment. This implementation really worked for me. Check my repositories on the subject: https://github.com/hidayatarg/Asp.net-Core-2.1-Jwt-Authentication-Middleware
https://github.com/hidayatarg/Decode-JWT-Token






share|improve this answer























  • Hello, thank you for answering. I tried that method but then I needed a more customised function thats why I used the typefilter instead.
    – JianYA
    Nov 12 '18 at 22:14










  • Since Jwt sign the headers of our request. Here i am reading the header specifically the middle part jwt which is called the JWT pay load if you want to see the full exampl echeck my repo github.com/hidayatarg/…
    – Hidayat Arghandabi
    Nov 13 '18 at 7:36














1












1








1






I am recently doing a cooperative project on JWT. I wrote a middlware, when ever the user request to the api, It is checked by the Authentication middleware. I read the userRole from db and put it in the identity priciple I am sharing the middleware codes.



In here I read the JWT middle part to extract the user information



public class AuthenticationMiddleware
{
private readonly RequestDelegate _next;

// Dependency Injection
public AuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
string authHeader = context.Request.Headers["Authorization"];

if (authHeader != null)
{
int startPoint = authHeader.IndexOf(".") + 1;
int endPoint = authHeader.LastIndexOf(".");

var tokenString = authHeader.Substring(startPoint, endPoint - startPoint).Split(".");
var token = tokenString[0].ToString()+"==";

var credentialString = Encoding.UTF8
.GetString(Convert.FromBase64String(token));

// Splitting the data from Jwt
var credentials = credentialString.Split(new char { ':',',' });

// Trim this string.
var userRule = credentials[5].Replace(""", "");
var userName = credentials[3].Replace(""", "");

// Identity Principal
var claims = new
{
new Claim("name", userName),
new Claim(ClaimTypes.Role, userRule),

};
var identity = new ClaimsIdentity(claims, "basic");
context.User = new ClaimsPrincipal(identity);
}
await _next(context);
}


}


In startup.cs you need to call this middleware in the configure method



 app.UseMiddleware<AuthenticationMiddleware>();


In the controller



 [HttpGet("GetUsers")]
[Authorize(Roles = "admin")]
public ActionResult GetUsers()
{
var users = _authRepository.GetUsers();
return Ok(users);
}


if You need any help please give a comment. This implementation really worked for me. Check my repositories on the subject: https://github.com/hidayatarg/Asp.net-Core-2.1-Jwt-Authentication-Middleware
https://github.com/hidayatarg/Decode-JWT-Token






share|improve this answer














I am recently doing a cooperative project on JWT. I wrote a middlware, when ever the user request to the api, It is checked by the Authentication middleware. I read the userRole from db and put it in the identity priciple I am sharing the middleware codes.



In here I read the JWT middle part to extract the user information



public class AuthenticationMiddleware
{
private readonly RequestDelegate _next;

// Dependency Injection
public AuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
string authHeader = context.Request.Headers["Authorization"];

if (authHeader != null)
{
int startPoint = authHeader.IndexOf(".") + 1;
int endPoint = authHeader.LastIndexOf(".");

var tokenString = authHeader.Substring(startPoint, endPoint - startPoint).Split(".");
var token = tokenString[0].ToString()+"==";

var credentialString = Encoding.UTF8
.GetString(Convert.FromBase64String(token));

// Splitting the data from Jwt
var credentials = credentialString.Split(new char { ':',',' });

// Trim this string.
var userRule = credentials[5].Replace(""", "");
var userName = credentials[3].Replace(""", "");

// Identity Principal
var claims = new
{
new Claim("name", userName),
new Claim(ClaimTypes.Role, userRule),

};
var identity = new ClaimsIdentity(claims, "basic");
context.User = new ClaimsPrincipal(identity);
}
await _next(context);
}


}


In startup.cs you need to call this middleware in the configure method



 app.UseMiddleware<AuthenticationMiddleware>();


In the controller



 [HttpGet("GetUsers")]
[Authorize(Roles = "admin")]
public ActionResult GetUsers()
{
var users = _authRepository.GetUsers();
return Ok(users);
}


if You need any help please give a comment. This implementation really worked for me. Check my repositories on the subject: https://github.com/hidayatarg/Asp.net-Core-2.1-Jwt-Authentication-Middleware
https://github.com/hidayatarg/Decode-JWT-Token







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 7:38

























answered Nov 12 '18 at 10:20









Hidayat Arghandabi

119




119












  • Hello, thank you for answering. I tried that method but then I needed a more customised function thats why I used the typefilter instead.
    – JianYA
    Nov 12 '18 at 22:14










  • Since Jwt sign the headers of our request. Here i am reading the header specifically the middle part jwt which is called the JWT pay load if you want to see the full exampl echeck my repo github.com/hidayatarg/…
    – Hidayat Arghandabi
    Nov 13 '18 at 7:36


















  • Hello, thank you for answering. I tried that method but then I needed a more customised function thats why I used the typefilter instead.
    – JianYA
    Nov 12 '18 at 22:14










  • Since Jwt sign the headers of our request. Here i am reading the header specifically the middle part jwt which is called the JWT pay load if you want to see the full exampl echeck my repo github.com/hidayatarg/…
    – Hidayat Arghandabi
    Nov 13 '18 at 7:36
















Hello, thank you for answering. I tried that method but then I needed a more customised function thats why I used the typefilter instead.
– JianYA
Nov 12 '18 at 22:14




Hello, thank you for answering. I tried that method but then I needed a more customised function thats why I used the typefilter instead.
– JianYA
Nov 12 '18 at 22:14












Since Jwt sign the headers of our request. Here i am reading the header specifically the middle part jwt which is called the JWT pay load if you want to see the full exampl echeck my repo github.com/hidayatarg/…
– Hidayat Arghandabi
Nov 13 '18 at 7:36




Since Jwt sign the headers of our request. Here i am reading the header specifically the middle part jwt which is called the JWT pay load if you want to see the full exampl echeck my repo github.com/hidayatarg/…
– Hidayat Arghandabi
Nov 13 '18 at 7:36


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256440%2fasp-net-core-jwt-implement-signinmanager-claims%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini