Tôi đang cố triển khai Xác thực Mẫu trong Ứng dụng của mình. Tôi ví dụ khác nhau và nhìn vào các mẫu và câu hỏi được cung cấp trong diễn đàn này và ASP.net MVC nhưng tôi chỉ không thể làm cho nó hoạt động.Xác thực Mẫu: Vai trò (MVC 4) C#
tôi quản lý để xác thực người dùng của tôi, nhưng vai trò dường như không làm việc :-(
tôi đã thiết lập Web.Config của tôi như sau:
<authentication mode="Forms"> <forms loginUrl="~/Account/Login" timeout="2880" /> </authentication>
Trong điều khiển của tôi tôi đặt Trang chỉ mục cho AllowAnonymous và sau đó đăng ký tại đó nếu người dùng được xác thực. Nếu không, sau đó chuyển hướng đến trang đăng nhập ..
[AllowAnonymous]
public ActionResult Index(string sortOrder, string searchString,string currentFilter, int? page)
{
if (!Request.IsAuthenticated)
{
return RedirectToAction("Login", "Account");
}
//Find all the employees
var employees = from s in db.Employees
select s;
//Pass employees to the view (All works fine)
return View(employees.ToPagedList(pageNumber, pageSize));
}
tất cả đang làm việc 100%
đang đăng nhập của tôi trông như thế này:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(User user, string returnUrl)
{
var myUser = db.Users.Where(b => b.UserName == user.UserName).FirstOrDefault();
if(myUser != null)
{
if(myUser.Password==user.Password)
{
//These session values are just for demo purpose to show the user details on master page
//Session["User"] = user;
ICollection<UserAccessLevel> levels = db.UserAccessLevels.Where(b => b.UserId == myUser.UserId).ToList();
//Session["levels"] = levels;
//Let us now set the authentication cookie so that we can use that later.
FormsAuthentication.SetAuthCookie(user.UserName, false);
return RedirectToAction("Index","Employee");
}
}
ViewBag.Message = "Invalid User name or Password.";
return View(user);
}
Tôi cũng có đoạn mã sau trong file Global.asax:
protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
{
if (FormsAuthentication.CookiesSupported == true)
{
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
try
{
//let us take out the username now
string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
string roles = string.Empty;
using (TrainingContext entities = new TrainingContext())
{
User user = entities.Users.SingleOrDefault(u => u.UserName == username);
roles = "admin";//user.Roles;
}
//Let us set the Pricipal with our user specific details
e.User = new System.Security.Principal.GenericPrincipal(
new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
}
catch (Exception)
{
//somehting went wrong
}
}
}
}
Khi tôi đăng nhập vào thực thi FormsAuthentication_OnAuthenticate của tôi và mọi thứ đều tốt. Người dùng của tôi được đặt và vai trò của tôi trong phiên cũng có ...
Nhưng khi tôi nhấp vào chi tiết màn hình Nhân viên/chỉ mục, tôi sẽ quay lại màn hình đăng nhập (tôi hy vọng nó sẽ đưa tôi đến chi tiết về nhân viên mà tôi đã nhấp vào vì tôi đã đăng nhập và tôi đang thiết lập vai trò quản trị)
Vui lòng giúp tôi thử và khắc phục sự cố. Tôi ngồi trong hơn 18 giờ đã cố gắng tìm ra điều này.
Tôi đã xem xét các giải pháp và như bạn có thể thấy hầu hết các mã của tôi xuất phát từ đó ... codeproject.com/Articles/578374/AplusBeginner-27splusTutorialplusonplusCustomplusF codeproject.com/Articles/342061/Understanding-ASP- NET-Vai trò và Thành viên-A-Bắt đầu codeproject.com/Articles/408306/Understanding-and-Implementing-ASP-NET-Custom-Form
trong trường hợp bạn cần thêm chi tiết về mã của tôi, bạn cũng có thể tải xuống từ GitHub https://github.com/Ruandv/Training/tree/FormsAuthentication
Tôi sẽ đánh giá cao sự trợ giúp của bạn.
Bạn có thể đăng bài không có trong lớp thuộc tính ủy quyền của mình không? –
Tôi không có lớp thuộc tính Authorize tùy chỉnh. Tôi đang sử dụng tiêu chuẩn. – Gremlin1708