2

私はこれらの4つのクラスを持っています:

public class Personal
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

public class LoginRepository
    {
        Context context = new Context();
        public Personal GetByUsernameAndPassword(Personal user)
        {
            return context.Personals.Where(u => u.Name==user.Name).FirstOrDefault();
        }
    }

public class LoginApplication
    {
        LoginRepository userRepo = new LoginRepository();
        public Personal GetByUsernameAndPassword(Personal user)
        {
            return userRepo.GetByUsernameAndPassword(user);
        }
    }

public class SessionContext
    {
        public void SetAuthenticationToken(string name, bool isPersistant, Personal userData)
        {
            string data = null;
            if (userData != null)
                data = new JavaScriptSerializer().Serialize(userData);

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, name, DateTime.Now, DateTime.Now.AddYears(1), isPersistant, data);

            string cookieData = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieData)
            {
                HttpOnly = true,
                Expires = ticket.Expiration
            };

            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public Personal GetUserData()
        {
            Personal userData = null;

            try
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
                if (cookie != null)
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);

                    userData = new JavaScriptSerializer().Deserialize(ticket.UserData, typeof(Personal)) as Personal;
                }
            }
            catch (Exception ex)
            {
            }

            return userData;
        }
    }

そして、私のコントローラーにはこれがあります:

 public class HomeController : Controller
    {
        LoginApplication userApp = new LoginApplication();
        SessionContext context = new SessionContext();
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(Personal user)
        {
            var authenticatedUser = userApp.GetByUsernameAndPassword(user);
            if (authenticatedUser != null)
            {
                context.SetAuthenticationToken(authenticatedUser.Name, false, authenticatedUser);
                return RedirectToAction("Index", "Asp");
            }
            return View();
        }
    }

しかし問題は、ログインに正しい名前を使用しても、次のエラーが表示されることです。

HTTP エラー 401.0 - 許可されていません このディレクトリまたはページを表示する権限がありません。

セッションが作成されていないと思います。私は何をすべきか?

4

1 に答える 1

3

リクエスト/ルーティングを正しく処理していないIIS構成のように聞こえるため、MVCルートを使用して適切なコントローラーを選択する代わりに、IISはディレクトリへのパスを確認し、ディレクトリのリストが無効になっているため、無許可をスローします。

その設定方法は、実行している IIS のバージョンによって異なります。技術的な観点からは構成は基本的に同じですが、管理コンソールが 6 から 7 に大幅に変更されたため、IIS7(+) でそれを行う方法は以前に尋ねられ、回答を書き直す代わりに、精神に役立つと思いますこのコミュニティの回答に転送する方が良い

于 2013-02-21T19:32:08.207 に答える