小编Rob*_*urd的帖子

如何扩展User.Identity的可用属性

我正在使用MVC5 Identity 2.0登录我的网站,其中身份验证详细信息存储在SQL数据库中.Asp.net Identity已经以标准方式实现,可以在许多在线教程中找到.

IdentityModels中的ApplicationUser类已扩展为包含一些自定义属性,例如整数OrganizationId.我们的想法是,可以创建许多用户并将其分配给公共组织以实现数据库关系.

public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

        //Extended Properties
        public DateTime? BirthDate { get; set; }
        public long? OrganizationId { get; set; }

        //Key Mappings
        [ForeignKey("OrganizationId")]
        public virtual Organization Organization { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

如何从控制器中检索当前登录用户的OrganizationId属性?一旦用户登录,这是否可通过方法获得,或者每次执行控制器方法时,我是否始终根据UserId从数据库中检索OrganizationId?

在网上阅读我看到我需要使用以下内容来登录UserId等.

using Microsoft.AspNet.Identity;
...
User.Identity.GetUserId();
Run Code Online (Sandbox Code Playgroud)

但是,OrganizationId不是User.Identity中可用的属性.我是否需要扩展User.Identity以包含OrganizationId属性?如果是这样,我该怎么做呢.

我经常需要OrganizationId的原因是许多表查询依赖于OrganizationId来检索与登录用户相关联的与组织相关的数据.

asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2

125
推荐指数
4
解决办法
10万
查看次数

如何检测是否按下任何键

如何检测当前是否正在按任何键盘键?我对关键是什么不感兴趣,我只是想知道是否仍然按下了任何键.

if (Keyboard.IsKeyDown(Key.ANYKEY??)
{

}
Run Code Online (Sandbox Code Playgroud)

c# wpf c#-4.0

6
推荐指数
2
解决办法
3万
查看次数

MVC View ViewModel HttpPost返回值始终为NULL

我通过表单HttpPost将ViewModel从我的View传递回Controller.但是,返回的值始终为NULL.

视图模型

public class vmCompanyAddress
{
    public StatelyTechAdmin.Models.Company Company { get; set; }
    public StatelyTechAdmin.Models.CompanyAddress Address { get; set; }

    public SelectList Counties { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

公司类模型

public class Company
{
    [Key]
    public virtual long CompanyId { get; set; }

    [Required]
    [Display(Name = "Company Name")]
    public virtual string Name { get; set; }

    public virtual DateTime CreatedDate { get; set; }

    public virtual IEnumerable<CompanyAddress> CompanyAddresses { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

CompanyAddress类模型

public class CompanyAddress
{
    [Key]
    public virtual …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc razor asp.net-mvc-4 asp.net-mvc-viewmodel

6
推荐指数
2
解决办法
1万
查看次数

如何迭代Excel工作表仅从特定列中提取数据

如何使用多个工作表迭代excel工作簿,只从"C","E"和"F"列中提取数据?

这是我到目前为止的代码:

public static string ExtractData(string filePath)
    {
        Excel.Application excelApp = new Excel.Application();
        Excel.Workbook workBook = excelApp.Workbooks.Open(filePath);

        string data = string.Empty;

        int i = 0;
        foreach (Excel.Worksheet sheet in workBook.Worksheets)
        {
            data += "*******   Sheet " + i++.ToString() + "   ********\n";

            //foreach (Excel.Range row in sheet.UsedRange.Rows)
            //{
            //    data += row.Range["C"].Value.ToString();
            //}

            foreach (Excel.Range row in sheet.UsedRange.Rows)
            {
                foreach (Excel.Range cell in row.Columns)
                {
                    data += cell.Value + "   ";
                }
                data += "\n";
            }
        }

        excelApp.Quit();

        return data;
    } …
Run Code Online (Sandbox Code Playgroud)

c# excel .net-4.0 excel-interop

5
推荐指数
2
解决办法
3万
查看次数

如何在MVC核心(1.0.0)中读取xml文档?

我是网络编程的新手,决定从.net 4.5切换到.net核心.

我的项目在以下位置有一个静态xml文档:

wwwroot文件/国家/ EN-GB.xml

如何在指定的路径上读取xml文件?最终我将数据转换为SelectList.

在.net 4.5中,我使用DataSet和HttpConext ... MapPath来读取不再适用于核心mvc的xml文档.

任何建议都非常欢迎.

asp.net-core-mvc asp.net-core asp.net-core-1.0

2
推荐指数
1
解决办法
8396
查看次数

WPF - 如何通过绑定来停止计时器

首先,感谢您抽出宝贵时间阅读这篇文章.

我有一个计时器类,每60秒从我的SQL数据库下载一次"产品".即检查可能已被其他用户编辑的更新产品.

这是我的班级代码:

public class GetProducts : INotifyPropertyChanged
    {
        public GetProducts()
        {
            Timer updateProducts = new Timer();
            updateProducts.Interval = 60000; // 60 second updates
            updateProducts.Elapsed += timer_Elapsed;
            updateProducts.Start();
        }

        public ObservableCollection<Products> EnabledProducts
        {
            get
            {
                return ProductsDB.GetEnabledProducts();
            }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("EnabledProducts"));
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
Run Code Online (Sandbox Code Playgroud)

然后我将其绑定到我的XAML(WPF)控件的tag属性:

<Page.Resources>
    <!-- Products Timer -->
    <products_timer:GetProducts x:Key="getProducts_timer" />
</Page.Resources>


Tag="{Binding Source={StaticResource getProducts_timer}, Path=EnabledProducts, Mode=OneWay}"
Run Code Online (Sandbox Code Playgroud)

这非常有效.我遇到的问题是,当托管控件的窗口或页面关闭时,无论如何,计时器都会继续打开.

一旦Page/Control不再可用,有人可以建议一种方法来停止自动收报机吗?

再次感谢您的时间.非常感谢所有帮助.

c# wpf xaml c#-4.0

1
推荐指数
1
解决办法
952
查看次数