在C#asp.net中获得验证用户的最佳方式

时间:2017-09-28 22:01:12

标签: c# asp.net login asp.net-mvc-5

关于如何处理这个问题,我有点不知所措,但希望你们这些人的意见能帮助我。

我试图只允许我的“首页/日历”网站的用户查看他们创建的内容。在我的脑海中,我需要进入每个创建,添加,删除和更新功能,并使用以下方式获取当前用户ID:

User.Identity.GetUserId();

然后,我会将此UserID与运行该函数时存储在表中的UserID进行比较。

这是我的表格的样子:

CREATE TABLE [dbo].[AspNetUserLogins] (
    [LoginProvider] NVARCHAR (128) NOT NULL,
    [ProviderKey]   NVARCHAR (128) NOT NULL,
    [UserId]        NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetUserLogins] PRIMARY KEY CLUSTERED ([LoginProvider] ASC, [ProviderKey] ASC, [UserId] ASC),
    CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);



    CREATE TABLE [dbo].[Events] (
    [EventID]     INT            IDENTITY (1, 1) NOT NULL,
    [Subject]     NVARCHAR (100) NOT NULL,
    [Description] NVARCHAR (300) NULL,
    [Start]       DATETIME       NOT NULL,
    [End]         DATETIME       NULL,
    [ThemeColor]  NVARCHAR (10)  NULL,
    [IsFullDay]   BIT            NOT NULL,
    [UserId]        NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK__Event__7944C8701D5AFFC5] PRIMARY KEY CLUSTERED ([EventID] ASC)
);

CREATE TABLE [dbo].[Links] (
    [Id]   INT            IDENTITY (1, 1) NOT NULL,
    [Name] VARCHAR (50)   NULL,
    [Link] VARCHAR (1000) NULL,
    [UserId]        NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK__tmp_ms_x__3214EC07BE58FD26] PRIMARY KEY CLUSTERED ([Id] ASC)
);

我希望有一个更简单或更优雅的解决方案。我最终会把这个项目变成一个训练营并向雇主展示。

这是我的家庭控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;



namespace HomePageCalendar.Controllers
{
    public class HomeController : Controller
    {
        private MyDatabaseEntities _context;

        public HomeController()
        {
            _context = new MyDatabaseEntities();
        }

        protected override void Dispose(bool disposing)
        {
            _context.Dispose();
        }
        public ActionResult New()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Links home)
        {
            _context.Links.Add(home);
            _context.SaveChanges();
            return RedirectToAction("Index", "home");
        }
        public ActionResult Index(string searchString)
        {
            var Link = from l in _context.Links
                       select l;

            if (!String.IsNullOrEmpty(searchString))
            {
                Link = Link.Where(s => s.Name.Contains(searchString));
            }

            var Name = _context.Links.ToList();
            return View(Link);

        }

        public ActionResult Remove()
        {
            ViewBag.title = "Remove Links";
            ViewBag.homes = _context.Links.ToList();
            return View();
        }

        [HttpPost]
        public ActionResult Remove(int[] cheeseIds)
        {
            foreach (int cheeseId in cheeseIds)
            {
                Links theCheese = _context.Links.Single(c => c.Id == cheeseId);
                _context.Links.Remove(theCheese);
            }

            _context.SaveChanges();

            return Redirect("/");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

0 个答案:

没有答案