In form authentication the [Authorize(Roles = "Admin")] not working.
Here is the web.config:
"true" /> -
"Forms" > -
"~/Account/Login" loginUrl="~/Account/Login" domain=".xyz.com" path="/"/> -
"395BB0DAFA02BA520EDB43E7EDF06BBFD72FC13A5209243270539E01074B0EA4" decryptionKey="037D2C9D97979D8D810F4A6A2B9337BD181F32167735F2E0" validation="SHA1"/>
Application_AuthenticateRequest in Global.asax- protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
- {
- HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
- if (authCookie != null)
- {
- try
- {
- FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- CustomPrincipalSerializeModel serializeModel = serializer.Deserialize
(authTicket.UserData); - CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
- newUser.UserID = serializeModel.UserID;
- newUser.FirstName = serializeModel.FirstName;
- newUser.LastName = serializeModel.LastName;
- newUser.ProfilePicture = serializeModel.ProfilePicture;
- newUser.UserCode = serializeModel.UserCode;
- newUser.UserEmail = serializeModel.UserEmail;
- newUser.UserType = serializeModel.UserType;
- newUser.Fk_Parent = serializeModel.Fk_Parent;
- newUser.CompanyID = serializeModel.CompanyID;
- newUser.isSASS = serializeModel.isSASS;
- newUser.Commission = serializeModel.Commission;
- newUser.CommissionManager = serializeModel.CommissionManager;
- newUser.ISACount = serializeModel.ISACount;
- HttpContext.Current.User = newUser;
- }
- catch (Exception ex)
- {
- HttpContext.Current.User = null;
- }
- }
- }
- interface ICustomPrincipal : IPrincipal
- {
- int UserID { get; set; }
- string FirstName { get; set; }
- string LastName { get; set; }
- string ProfilePicture { get; set; }
- Guid UserCode { get; set; }
- string UserEmail { get; set; }
- int UserType { get; set; }
- int Fk_Parent { get; set; }
- string CompanyID { get; set; }
- Nullable<bool> isSASS { get; set; }
- double? Commission { get; set; }
- double? CommissionManager { get; set; }
- Nullable<int> ISACount { get; set; }
- }
- public class CustomPrincipal : ICustomPrincipal
- {
- public IIdentity Identity { get; private set; }
- public bool IsInRole(string role) {
- string inRole = string.Empty;
- inRole =Enum.GetName(typeof(UserType), UserType);
- if (inRole == role)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public CustomPrincipal(string email)
- {
- this.Identity = new GenericIdentity(email);
- }
- public int UserID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string ProfilePicture { get; set; }
- public Guid UserCode { get; set; }
- public string UserEmail { get; set; }
- public int UserType { get; set; }
- public int Fk_Parent { get; set; }
- public string CompanyID { get; set; }
- public Nullable<bool> isSASS { get; set; }
- public double? Commission { get; set; }
- public double? CommissionManager { get; set; }
- public Nullable<int> ISACount { get; set; }
- }
- public class CustomPrincipalSerializeModel
- {
- public int UserID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string ProfilePicture { get; set; }
- public Guid UserCode { get; set; }
- public string UserEmail { get; set; }
- public int UserType { get; set; }
- public int Fk_Parent { get; set; }
- public string CompanyID { get; set; }
- public Nullable<bool> isSASS { get; set; }
- public double? Commission { get; set; }
- public double? CommissionManager { get; set; }
- public Nullable<int> ISACount { get; set; }
- }
- [Authorize(Roles = "Admin,SubAdmin")]
- public ActionResult Index()
- {
- ////
- }
the problem is that when i use in web.config then Authorize Role is not working.
I have two different domain(one is suppose xyz.com and another one is a.xyz.com(subdomain))
Here i am passing the cookie value from one to another for accessing all the data.(sso)
The above code is in xyz.com. the same code of global.asax is on the a.xyz.com so How do i fix the problem.Both are in Mvc 5 c#.
Jitendra ShekhawatPosted Apr 10, 2017, 8:11 AM
Asma KhalidPosted Apr 10, 2017, 6:45 AM