0

I need to save New Claims value to my SQL Database [AspNetUserClaims] table which I suppose can be done like this (please correct if I am wrong as I'm new to MVC).

The User logs in using OAuth from Facebook. Once logged in, he fills in some form that collects user data as well as some uploads etc using a wizard type setup. Once the multi step wizard is completed in the View, at the controller after POST these fields are saved to the database. I need to save some of the fields data to the [AspNetUserClaims] table which I suppose is possible in this way:

//Post Fields
var db = new ApplicationDbContext();
var Post = new PostModel();
Post.PostTitle = model.Post.PostTitle;
Post.PostBrief = model.Post.PostBrief;
db.PostModel.Add(Post);
db.SaveChanges();


// User Claims Data
var claims = new List<System.Security.Claims.Claim>();
claims.Add(new System.Security.Claims.Claim("FullName", model.UserClaims.FullName));
claims.Add(new System.Security.Claims.Claim("Email", model.UserClaims.Email));
System.Security.Claims.ClaimsIdentity i = (System.Security.Claims.ClaimsIdentity)HttpContext.GetOwinContext().Authentication.User.Identity;
i.AddClaims(claims);

//How to make the changes to User Claims, save to the database?
Cyberpks
  • 1,401
  • 6
  • 21
  • 51
  • You're calling db.SaveChanges() before adding the claims? – Rafael Companhoni Feb 11 '16 at 15:02
  • No, that actually below the claims part (I've done that using a Transaction) but, does that make any difference? – Cyberpks Feb 11 '16 at 15:11
  • 1
    Possible duplicate of [How to add claims in ASP.NET Identity](http://stackoverflow.com/questions/20383955/how-to-add-claims-in-asp-net-identity) – kloarubeek Feb 11 '16 at 20:34
  • @kloarubeek, Not a duplicate(tried that before posting the question), or you can say not working for me atleast. – Cyberpks Feb 12 '16 at 02:31
  • That questions perfectly explains how to add claims. You already add claims, I think if you sign in (as explained in that question), the claims are added to the database and the cookie. – kloarubeek Feb 12 '16 at 11:19

0 Answers0