如何在ASP.NET MVC中保存身份验证ID的设置

时间:2018-11-24 10:47:31

标签: asp.net-mvc authentication model-view-controller

我制作了一个电视指南,您可以在其中看到5个频道的节目。如果您登录,也可以制作“个人”电视指南。在相应的5个渠道中,您可以通过单击复选框来决定要查看其概况。如何使应用程序记住用户输入。假设我登录并打开了通道1(SVT1)和通道2(SVT2)的菜单。然后,我希望在下次使用该用户ID登录时也打开这两个通道,该怎么做?

Screenshot of my personal tv-guide page

我的控制器

public class FavoritChannelsController : Controller
{
    TvProgramDBEntities db = new TvProgramDBEntities();

    [Authorize(Roles = "User")]
    public ActionResult channel_index()
    {
        List<Full> model = new List<Full>();
        var list = db.Full.Where(d => d.Date == "2018-11-12").ToList();

        foreach (var item in list)
        {
            if (item.Id <1000)
            {
                var initedF = new Full
                {
                    Channel = Regex.Replace(item.Channel, @"\s", ""),
                    Program = item.Program,
                    Time = item.Time,
                    Date = Regex.Replace(item.Date, @"\s", "")
                };
                model.Add(initedF);
            }

        }
        return View(model);
    }
}

我的观点

@model IEnumerable<Uppgift4.Models.Full>

@{
ViewBag.Title = "channel_Index";
var list = Model.ToList();
var list1 = list.Where(_ => _.Channel == "SVT1").Select(_ =>  _.Time + 
_.Program).ToList();
var list2 = list.Where(_ => _.Channel == "SVT2").Select(_ => _.Time + 
_.Program).ToList();
var list3 = list.Where(_ => _.Channel == "TV3").Select(_ => _.Time + 
_.Program).ToList();
var list4 = list.Where(_ => _.Channel == "TV4").Select(_ => _.Time + 
_.Program).ToList();
var list5 = list.Where(_ => _.Channel == "Kanal5").Select(_ => _.Time + 
_.Program).ToList();
}

(_ => _.Date + " " +_.Time + _.Program).ToList();

<style>
.hiddenRow {
    padding: 0 !important;
}

.in-line {
    display: inline;
}
</style>
<br />

<table class="table table-condensed" style="border-collapse:collapse;">
   <thead>
    <tr>
        <th>
            <h3>TV-Tablå 2018-11-12</h3>
            <b>Välj de kanaler du vill se tablån för</b>
        </th>
        <th>

        </th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>
            <input type="checkbox" data-toggle="collapse" data-target="div
[id*='demo1']" /><b> SVT1</b> 
        </td>
    </tr>
    @for (var i = 0; i < @list1.Count; i++)
    {
        <tr>
            <td colspan="2" class="hiddenRow">
                <div id="demo1+'@i'+" class="accordian-body collapse">
                    @list1[i]
                </div>
            </td>
        </tr>
    }

    <tr data-toggle="collapse" data-target="#demo2">
        <td>
            <input type="checkbox" data-toggle="collapse" data-target="div
[id*='demo2']" /><b> SVT2</b> 
        </td>
    </tr>
    @for (var i = 0; i < @list2.Count; i++)
    {
        <tr>
            <td colspan="2" class="hiddenRow">
                <div id="demo2+'@i'+" class="accordian-body collapse">
                    @list2[i]
                </div>
            </td>
        </tr>
    }

    <tr data-toggle="collapse" data-target="#demo3">
        <td>
            <input type="checkbox" data-toggle="collapse" data-target="div
[id*='demo3']" /><b> TV3</b> 
        </td>
    </tr>
    @for (var i = 0; i < @list3.Count; i++)
    {
        <tr>
            <td colspan="2" class="hiddenRow">
                <div id="demo3+'@i'+" class="accordian-body collapse">
                    @list3[i]
                </div>
            </td>
        </tr>
    }

    <tr data-toggle="collapse" data-target="#demo4">
        <td>
            <input type="checkbox" data-toggle="collapse" data-target="div
[id*='demo4']" /><b> TV4</b> 
        </td>
    </tr>
    @for (var i = 0; i < @list4.Count; i++)
    {
        <tr>
            <td colspan="2" class="hiddenRow">
                <div id="demo4+'@i'+" class="accordian-body collapse">
                    @list4[i]
                </div>
            </td>
        </tr>
    }

    <tr data-toggle="collapse" data-target="#demo5">
        <td>
            <input type="checkbox" data-toggle="collapse" data-target="div
[id*='demo5']" /><b> Kanal5</b> 
        </td>
    </tr>
    @for (var i = 0; i < @list5.Count; i++)
    {
        <tr>
            <td colspan="2" class="hiddenRow">
                <div id="demo5+'@i'+" class="accordian-body collapse">
                    @list5[i]
                </div>
            </td>
        </tr>
    }
</tbody>
</table>

我的模型(来自我的数据库表,带有实体框架)

namespace Uppgift4.Models
{
using System;
using System.Collections.Generic;

public partial class Full
{
    public int Id { get; set; }
    public string Channel { get; set; }
    public string Program { get; set; }
    public string Category { get; set; }
    public string Date { get; set; }
    public string Time { get; set; }
    public string Length { get; set; }
  }
 }

1 个答案:

答案 0 :(得分:0)

通过使用以下命令,您可以获取控制器中当前用户的UserId。

using Microsoft.AspNet.Identity;



User.Identity.GetUserId()

使用此ID保存/检索用户选择。例如,您可以添加以下实体。

public class User_Channel
{
    public int User_ChannelId { get; set; }
    [MaxLength(128)]
    public string UserId { get; set; }
    public int ChannelId { get; set; }
}