小编Sha*_*hin的帖子

防止多次登录

我试图在我的应用程序中使用相同的用户阻止多次登录.
我的想法是在用户登录时更新安全标记并将其添加为Claim,然后在每个请求中将Cookie中的标记与数据库中的标记进行比较.这就是我实现的方式:

        public virtual async Task<ActionResult> Login([Bind(Include = "Email,Password,RememberMe")] LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        SignInStatus result =
            await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false);
        switch (result)
        {
            case SignInStatus.Success:
                var user = UserManager.FindByEmail(model.Email);
                var id = user.Id;
                UserManager.UpdateSecurityStamp(user.Id);
                var securityStamp = UserManager.FindByEmail(model.Email).SecurityStamp;
                UserManager.AddClaim(id, new Claim("SecurityStamp", securityStamp));
Run Code Online (Sandbox Code Playgroud)

然后在身份验证配置中我添加了

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = ctx =>
                {
                    var ret = Task.Run(() =>
                    {
                        Claim claim = …
Run Code Online (Sandbox Code Playgroud)

cookies asp.net-mvc asp.net-identity

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

在部分回发ASP.NET上维护面板滚动位置

我有一个在ASP.NET Panel中放置的gridview.面板和Gridview都在UpdatePanel中.gridview中有一列导致部分PostBacks.我想在这些回发上保持Panel Scroll位置.有什么办法吗?问候.

asp.net postback scroll updatepanel

20
推荐指数
2
解决办法
4万
查看次数

实体框架:在运行时更改连接字符串

假设有一个ASP.NET MVC应用程序使用Entity Framework 6,代码优先方法和StructureMap作为IoC.
它还使用工作单元模式.以下是代码:

域类

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }        
 
    }
Run Code Online (Sandbox Code Playgroud)

IUnitOfWork和DbContext:

    public interface IUnitOfWork
{
    IDbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveChanges();
}

public class Sample07Context : DbContext, IUnitOfWork
{
    public DbSet<Product> Products { set; get; }

    #region IUnitOfWork Members

    public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
    {
        return base.Set<TEntity>();
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

服务类中的业务逻辑:

   public interface …
Run Code Online (Sandbox Code Playgroud)

c# structuremap asp.net-mvc entity-framework dependency-injection

18
推荐指数
3
解决办法
4万
查看次数

模拟AsNoTracking实体框架

我如何模拟AsNoTracking方法?
在下面的示例中,DbContext已注入服务类.如果我从GetOrderedProducts方法中删除AsNoTracking扩展方法,但是AsNoTracking测试失败,因为它返回null,它工作正常.我也尝试过模拟AsNoTracking以返回正确的值,但它没有用.

public interface IUnitOfWork
{
    IDbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveAllChanges();
}

public class Entites : DbContext, IUnitOfWork
{
    public virtual DbSet<Product> Products { get; set; }  // This is virtual because Moq needs to override the behaviour

    public new virtual IDbSet<TEntity> Set<TEntity>() where TEntity : class   // This is virtual because Moq needs to override the behaviour 
    {
        return base.Set<TEntity>();
    }

    public int SaveAllChanges()
    {
        return base.SaveChanges();
    }
}

    public class ProductService
{
    private readonly IDbSet<Product> …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing entity-framework moq mocking

18
推荐指数
2
解决办法
6367
查看次数

捕获特定异常

如何使用c#捕获特定的异常?
在我的数据库中,某些列上有唯一索引.
当用户插入重复记录时,抛出此异常:

无法在对象'dbo中插入重复的键行.BillIdentity'具有独特的指数'IX _BillIdentity'.该语句已终止.

我怎么能抓住这个例外?
目前我正在检查使用此代码:

 catch (Exception ex) {
    if (ex.Message.Contains("Cannot insert duplicate key row in object 'dbo._BillIdentity' with unique index 'IX__BillIdentity")) {
        string ScriptKey = "$(function() {ShowMessage('Error');});";
        ScriptManager.RegisterStartupScript(Page, GetType(), "script", ScriptKey, true);
    }
}
Run Code Online (Sandbox Code Playgroud)

我觉得它的味道很难闻.
有没有更好的方法?

c# refactoring exception-handling

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

添加按钮到jquery ui对话框

我无法在此jquery ui对话框中添加按钮.如果可能的话请举个例子.谢谢.

<script type="text/javascript">
    $(document).ready(function () {
        //setup new person dialog
        $('#dialog2').dialog({
            autoResize: true,
            show: "clip",
            hide: "clip",
            height: 'auto',
            width: '1000',
            autoOpen: false,
            modal: true,
            position: 'top',
            draggable: false,
            title: "?????? ???????",
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });

        $('#viewfaktor').dialog({
            autoResize: true,
            show: "clip",
            hide: "clip",
            height: 'auto',
            width: '1000',
            autoOpen: false,
            modal: true,
            position: 'top',
            draggable: true,
            title: "?????? ???? ???",
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });


        $('#msgBox').dialog({


            autoResize: true,
            show: "clip",
            hide: "clip",
            height: …
Run Code Online (Sandbox Code Playgroud)

jquery jquery-ui jquery-ui-dialog

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

在MousePosition中打开JQuery Ui对话框

我想在鼠标位置打开JQuery UI对话框.我的代码有什么问题?

<script type="text/javascript">

    $(document).ready(function () {
        var x;
        var y;
        $(document).mousemove(function (e) {
            x = e.pageX;
            y = e.pageY;
        });

        $("#d").dialog({
            autoOpen: false,
            show: "blind",
            hide: "explode",
            position: [x, y]
        });
        $("#c").bind("mouseover", function () {
            $("#d").dialog('open'); // open
        });


        $("#c").bind("mouseleave", function () {
            $("#d").dialog('close'); // open
        });



    });



</script>
Run Code Online (Sandbox Code Playgroud)

jquery jquery-ui jquery-ui-dialog

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

如何在页面底部放置div

如何<div>使用CSS或jQuery 将a的位置设置到页面底部?

html css jquery position

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

PowerShell脚本构建Visual Studio项目

是否有任何powershell脚本可以在没有开放视觉工作室的情况下构建visual studio项目?

powershell visual-studio

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

检查ASP.NET CheckboxList中的多个项目

我尝试检查ASP.NET CheckboxList中的多个值,但我不能.
我写 :

chkApplications.SelectedValue = 2;
chkApplications.SelectedValue = 6;
Run Code Online (Sandbox Code Playgroud)

但它只选择值为'6'的项目有
什么问题?

c# asp.net checkboxlist

9
推荐指数
3
解决办法
4万
查看次数