asp.net-web-api


User claim update not effected in ASP.NET Identity?


i need to update the user claim in web api after the user logged in.
but after updating the user claim it will still return previous values.
bellow code used to update active user group after the user logged in.
/// <summary>
/// The class AppUser
/// </summary>
public class AppUser : ClaimsPrincipal
{
/// <summary>
/// Initializes a new instance of the <see cref="AppUser"/> class.
/// </summary>
/// <param name="principal">The principal.</param>
public AppUser(ClaimsPrincipal principal)
: base(principal)
{
}
/// <summary>
/// Gets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name
{
get
{
return this.FindFirst(ClaimTypes.Name).Value;
}
}
/// <summary>
/// Gets the name of the user.
/// </summary>
/// <value>
/// The name of the user.
/// </value>
public string UserName
{
get
{
return this.FindFirst("UserName").Value;
}
}
/// <summary>
/// Gets the active group.
/// </summary>
/// <value>
/// The active group.
/// </value>
public string ActiveGroup
{
get
{
return ((ClaimsIdentity)this.Identity).FindFirst("ActiveGroup").Value;
}
}
/// <summary>
/// Gets the email.
/// </summary>
/// <value>
/// The email.
/// </value>
public string Email
{
get
{
return this.FindFirst("Email").Value;
}
}
}
/// <summary>
/// The class BaseController
/// </summary>
public class BaseController : ApiController
{
/// <summary>
/// Gets the current user.
/// </summary>
/// <value>
/// The current user.
/// </value>
public AppUser CurrentUser
{
get
{
return new AppUser(this.User as ClaimsPrincipal);
}
}
}
public class AccountController : BaseController
{
[HttpPost]
[Route("UpdateUserGroup")]
public int UpdateUserGroup(string userGroup)
{
var user = User as ClaimsPrincipal;
var identity = user.Identity as ClaimsIdentity;
identity.RemoveClaim(identity.FindFirst("ActiveGroup"));
identity.AddClaim(new Claim("ActiveGroup", this.GetRoleNameByPresenter(userGroup)));
return 1;
}
}
The problem is that the claims are used in the authentication process and are part of the authentication token/cookie. If you want to remove a claim from the current user then you need to make sure the client get a new token/cookie.
If you're running for example bearer tokens with your api then you need to generate a new token and return that token to the client from your UpdateUserGroup(). The client then need to use the new token the next time it makes a request to the api.

Related Links

OWIN Authentication Failing - Web API
Custom Async Action Filter for Web API 2
ASP.NET Identity Bearer Token vs JWT Pros and Cons
Post data through Url to webapi
Getting error when expanding navigation property that contains a nested navigation property
Web Api Owin: How to “remap/rename” the fields in the json response created by BaseValidatingContext.SetError
Authentication + authorization from web api with adfs and saml 2
From route configuration to attribute routing
Swagger : List endpoints which are in another dll
Does windows 10 connection limit apply to self-hosted applications?
Signalr disconnected frequently while loading chat history in ionic
How do I override OnAuthorization in net core web api?
setting HttpContext.Current.User from JWT Token
change root url of web api project
Microservices vs multi-layered architecture
Return external classes from web api get

Categories

HOME
gdb
android-studio-2.2
object
fftw
apiblueprint
switch-statement
google-cloud-datalab
opengl-3
character
augeas
do-while
http-post
package-managers
standards
mvc-mini-profiler
nodemcu
delphi-xe5
visual-glitch
pimcore
beta
angular-translate
bioinformatics
aws-codecommit
angularjs-material
fstream
android-gridview
sharding
html-encode
projects-and-solutions
bc
apollostack
dbcontext
magic-draw
aquamacs
mach-o
cpu-cache
template-toolkit
google-photos
baidu
android-instrumentation
stripe-connect
latin
dotnetnuke-7
angular-ui-typeahead
nss
bizagi
boost-serialization
upx
realsense
qbxml
contentcontrol
jaunt-api
state-restoration
custom-errors
themoviedb-api
django-urls
android-launcher
slash
liquid-layout
bufferedinputstream
jbake
crowd
genymotion-call
template-deduction
spring-remoting
cfcache
notorm
windows-phone-8-sdk
printdialog
beaker-testing
preload
qi4j
paginator
objectquery
multiplexing
box2d-iphone
expression-blend-4
google-ajax-api
lr
mbeans
inline-formset
svk
object-database
cfwindow
sun
scrubyt
lemmatization
leader
idatareader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App