Question

May be there are many dublicates of this question. But I ignored default membership of MVC and use my custom login with cookies. That is why can not create Custom Authorize Attribute like in samples. I have User model like this:

public class MyUserModel
{
 public string Id{ get; set; }
 public string UserName { get; set; }
 public string Password { get; set; }
 public bool RememberMe { get; set; }
 public bool RoleId { get; set; }
}

Here is my LogIn action:

[HttpPost]
public ActionResult LogIn(MyUserModel model)
{
    if ((Request.Browser.Cookies))
       {
        if ((Request.Cookies["UserInfo"] == null))
         {
          if (model.RememberMe)
           {                           
            Response.Cookies["UserInfo"].Expires = DateTime.Now.AddDays(30);
           }
           else
           {
            Response.Cookies["UserInfo"].Expires = DateTime.Now.AddMinutes(Session.Timeout);
            }
              Response.Cookies["UserInfo"]["UserName"] = model.UserName;
              Response.Cookies["UserInfo"]["Password"] = model.Password;
              Response.Cookies["UserInfo"]["Id"] = model.Id.ToString();
            }                    
            else
            {
              Response.Cookies["UserInfo"]["UserName"] = model.UserName;
              Response.Cookies["UserInfo"]["Password"] = model.Password;
              Response.Cookies["UserInfo"]["Id"] = model.Id.ToString();
            }
         }
 return RedirectToAction("Index", "Home");
}

I need to create Custom Authorize Attribute by RoleId. By the way, I have methods as bool UserInRole(roleId), bool IsUserAuthenticated() is ready.

Sorry for bad English.

Was it helpful?

Solution

This tutorial is great.You can learn custom authorize clearly on this video. http://www.youtube.com/watch?feature=player_embedded&v=BsxUsyMSGeA

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top