Asp.Net身份-在运行时设置CookieDomain [英] Asp.Net Identity - Setting CookieDomain at runtime

查看:268
本文介绍了Asp.Net身份-在运行时设置CookieDomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要从Request.Url或数据库中存储的某些设置中提取此值,如何在运行时在CookieAuthenticationOptions中设置CookieDOmain?

How can I set the CookieDOmain in the CookieAuthenticationOptions at runtime if i want to pull this value from the Request.Url or from some settings stored in my database?

我想支持子域,但也要支持多租户,每个租户都有不同的域.

I want to support sub-domains, but also support multi-tenants too which each have different domains.

目前已配置完毕,我无权访问其中任何一个.

At the moment this is configured I don't have access to either of these.

保罗

推荐答案

您可以分配自己的cookie提供程序:

You can assign your own cookie provider:

CookieAuthProvider myProvider = new CookieAuthProvider();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
   AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
   LoginPath = new PathString("/Account/Login"),
   Provider = myProvider
});

要么实现自己的实现,要么直接继承现有的提供者:

Either implement your own, or simply inherit from the existing provider:

public class CookieAuthProvider : CookieAuthenticationProvider
{
    public override void ResponseSignIn(CookieResponseSignInContext context)
    {
      //Alter you cookie options
      //context.CookieOptions.Domain  =  "www...";      
      base.ResponseSignIn(context);
    }
 }

并实现ResponseSignIn,当端点在将其转换为cookie之前已提供登录信息时,将调用它.通过实施此方法,可以更改票证中的索赔和其他信息.

And implement ResponseSignIn, it is called when an endpoint has provided sign in information before it is converted into a cookie. By implementing this method the claims and extra information that go into the ticket may be altered.

将为您传递一个CookieResponseSignInContext,它公开了CookieOptions属性,可以在ResponseSignIn调用期间替换或更改该属性.

You'll be passed a CookieResponseSignInContext, which exposes CookieOptions property that can be replaced or altered during the ResponseSignIn call.

Katana项目中的代码参考:

Code references from Katana project:

CookieResponseSignInContext

CookieAuthenticationHandler

这篇关于Asp.Net身份-在运行时设置CookieDomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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