我可以以编程方式确定数据库"上下文中,QUOT;根据用户凭证使用? [英] Can I programmatically determine the database "context" to use based on user credentials?

本文介绍了我可以以编程方式确定数据库"上下文中,QUOT;根据用户凭证使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续的问题<一href=\"http://stackoverflow.com/questions/21341636/how-can-i-tell-the-web-api-castle-windsor-routing-engine-to-use-a-different-da/21355365#21355365\">here,如果答案似乎是指一个过于复杂和过于具体(EF,这我没有使用 - 即使不使用ORM)。

This is a followup to the question here, where the answer seems to refer to an overly-complicated and overly-specific (EF, which I'm not using - not even using an ORM).

必须有比周围烟雾,镜子这种常见的场景更直接的方式,和巫术暗示在这个问题的答案。

There has to be a more straightforward way around this common scenario than the smoke, mirrors, and sorcery hinted at in that answer.

请注意:我是因为我没有使用EF包裹在括号中背景,所以它不是一个文字的DbContext我这里所说

Note: I encased "context" in parenthesis because I'm not using EF, so it is not a literal "dbcontext" that I'm talking about here.

所以我开始纳闷:莫非我设置为每个会话一个全局变量,当用户进行身份验证和授权

So I got to wondering: Could I set a global variable for each session when the user is authenticated and authorized?

例如,当用户进行身份验证/授权,我就知道哪个数据库上下文/内容应服了他。

e.g., when the user is authenticated/authorized, I would know which database context/contents should be served up to him.

所以看起来我可以在Global.asax.cs中'的Application_Start()方法设置的值,然后要么改变RepositoriesInstaller(实施IWindsorInstaller)班有条件注册基于用户和不同的具体资料库的数据是什么,他应该有OR有条件的地方code在混凝土Repository自身使用这样或那样的数据库实例。

So it seems I could set a value in Global.asax.cs' Application_Start() method and then either alter the RepositoriesInstaller (implementing IWindsorInstaller) class to conditionally register different concrete Repositories based on the user and what data he should have OR place conditional code in the concrete Repository itself to use this or that database instance.

这是可行的?是单向的(改变RepositoriesInstaller /改变具体的库类)preferred?

Is this feasible? Is one way (altering the RepositoriesInstaller / altering the concrete Repositories class) preferred?

推荐答案

看一看这个答案,以显示你如何基于一个键或值解析正确的存储库。

Have a look at this answer which will show you how to resolve the correct repository based on a key or value.

如果您想将它存储与授权用户,则需要简单的序列化数据,并将其存储在cookie的验证:

If you want to store it with the authorized user, you need to simple serialize the data and store it on the authenticated cookie:

创建模型重新present登录的信息:

Create a Model to represent the logged in information:

public class AuthenticatedMember
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public int SiteNumber { get; set; }
}

做这样的事情在你的控制器登录:

Do something like a login in your controller:

var authenticatedMember = MembershipManager.ValidateLogin(model.Email, model.Password);

var cookie = FormsAuthentication.GetAuthCookie(authenticatedMember.Id.ToString(), false);
var ticket = FormsAuthentication.Decrypt(cookie.Value);

var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, authenticatedMember.ToJson(), ticket.CookiePath);

cookie.Value = FormsAuthentication.Encrypt(newTicket);
Response.Cookies.Add(cookie);

然后用模型绑定反序列化AuthenticatedMember时,您需要:

Then use a model binder to deserialize the AuthenticatedMember when you require:

public class AuthenticatedMemberModelBinder : IModelBinder
{
    #region IModelBinder Members

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.Model != null)
        {
            throw new InvalidOperationException("Cannot update instances");
        }

        if (controllerContext.RequestContext.HttpContext.Request.IsAuthenticated)
        {
            var cookie = controllerContext
                .RequestContext
                .HttpContext
                .Request
                .Cookies[FormsAuthentication.FormsCookieName];

            if (null == cookie)
                return null;

            var decrypted = FormsAuthentication.Decrypt(cookie.Value);

            if (!string.IsNullOrEmpty(decrypted.UserData))
            {
                return JsonConvert.DeserializeObject<AuthenticatedMember>(decrypted.UserData);
            }
        }
        return null;
    }

    #endregion
}

这篇关于我可以以编程方式确定数据库&QUOT;上下文中,QUOT;根据用户凭证使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆