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

JSON.NET object array deserialization to known type class
Configure Application Permissions in Azure AD
Web API parameter binding not working
Rewrite Web API 2 Url
Translate OData queries to SQL
How to provide custom mediatype formats for OData Api
EF7: DBContext configuration on ASP.NET 5 Web-API
Can I have multiple web api dlls in the same site?
Customize WWW-Authenticate challenge header with Jwt Bearer Authentication middleware in WebAPI
System Query Option $filter in OData v4 (canonical functions)
How to build EDM model for OData Web API in runtime?
using Web API attribute routing and RoutePrefix
How does one define an (optional) navigation property (?$expand=) in code?
Can we use OAuth to authenticate the consumers in my Web API?
Items count in OData v4 WebAPI response
How can I get the parameters from the WebAPI so that I can use them in the SQL query

Categories

HOME
beautifulsoup
spark-streaming
developer-tools
codenvy
jmeter
alexa-skills-kit
socket.io
can
alarm
vxml
visual-studio-emulator
jtextpane
octave
google-chrome-app
enthought
resultset
why3
ycsb
filechannel
comsol
flash-player
flatpak
qsqlquery
gruntfile
coin3d
dynamic-sql
microsoft-dynamics-nav
feathersjs
pyramid
gmm
swagger-2.0
replication
poedit
ksh
ios10.2
perlbrew
carrierwave
twiml
r-grid
android-gridview
sammy.js
openwhisk
joomla2.5
getpixel
least-squares
processwire
dm-script
angular-http
drupal-theming
uiimage
strftime
procfile
xbmc
payload
template-toolkit
pybrain
materialized-path-pattern
node-request
simplepie
winusb
file-uri
greatest-n-per-group
tooleap
delta
hadoop-partitioning
html-to-pdf
spring-retry
rewrite
ons-api
dbaccess
filesplitting
appserver
nsmenu
ora-00904
xml-dsig
memset
radiant
android-audiorecord
gwt-openlayers
spring-3
embedded-fonts
modular
cascadingdropdown
iboutlet
thttpd
httponly
ilasm
entity-attribute-value
ikimagebrowserview
htdocs
android-holo-everywhere
jqote
message-passing
spread
feasibility
google-wave
software-lifecycle
pbcopy

Resources

Encrypt Message