在一个站点上为另一个站点创建经过asp.net身份验证的cookie [英] Create a asp.net authenicated cookie on 1 site for another

查看:154
本文介绍了在一个站点上为另一个站点创建经过asp.net身份验证的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个mvc4网站. 站点1"www.mydomain.com"是允许用户创建帐户的营销站点.

I have 3 mvc4 sites. Site 1 'www.mydomain.com' is a marketing site that allows a user to create an account.

站点2"https://businessnamewhatever.mydoman.com"用于企业登录和使用可用功能.

Site 2 'https://businessnamewhatever.mydoman.com' is used for the business to log in and use the available features.

站点3 WebApi项目"https://api.mydomain.com"

Site 3 WebApi project 'https://api.mydomain.com'

在营销站点中创建帐户后(通过调用webapi创建帐户),我想将其重定向到自动登录的第二个站点.

After creating an account within the marketing site (account create via calling webapi) I want to redirect them to 2nd site automatically logging them in.

这两个站点都具有表单身份验证设置,并且都使用在web.config身份验证部分中设置的自己的cookie名称

Both of the sites have forms authentication setup and both use theirown cookie names set within the web.config authentication section

在调用api后,在我的营销站点内,我需要设置auth cookie,以允许用户自动登录站点2

Within my marketing site after calling the api I need to set the auth cookie that will allow the user to automatically log into site 2

我认为这只是在我的营销网站中使用的一种情况

I thought it would be just a case of using from within my marketing site

FormsAuthentication.SetAuthCookie(userName,true,站点2上我的cookie的名称");

FormsAuthentication.SetAuthCookie(userName, true,"the name of my cookie on site 2");

我的第二个主意是像这样进行肮脏的重定向

My 2nd idea was to do a dirty redirect like

   var url = string.Format("https://{0}.mydomain.com/account/sign-in?userName={1}&password={2}", model.BusinessName, model.BusinessName, model.Password);
   return Redirect(url);

但是,重定向永远不会发生.在网络"标签中,我可以看到响应,如果单击它,则可以正确重定向到该站点.

However the redirect never happens. Looking in the network tab I can see the response and if I click on it I am redirected to the site correctly.

有更好的方法吗?

更新

抱歉!我忘了提到我有第三个应用程序作为我的API

SORRY! I forgot to mention I have a 3rd application working as my API

我已经按照Kenneth的建议更新了所有的web.config.通过身份验证设置添加相同的机器密钥和域名

I have updated all of the web.config as suggested by Kenneth. adding a the same machine key and domain name with authentication settings

在营销网站中调用create user方法之后,应该尝试在其中设置cookie还是应该做其他事情?

After I have called the create user method within the marketing site should I be trying to set the cookie there or should I be doing something else?

推荐答案

首先,我假设两种产品都位于单独的域中(否则,您可以参考@Kenneth的回答),然后可以更改的源代码.这两个网站.

Firstly, I'm assuming both products are on separate domains (otherwise you can refer to @Kenneth's answer), and you can make changes to the source code of both sites.

  • 在营销站点上创建帐户之后,从服务器端向业务站点发送POST(使用SSL进行安全保护),其中包含用户名,密码哈希和重定向回营销URL.
  • 企业站点将存储这些值(视情况而定),然后以一次性使用的URL进行响应以重定向客户端浏览器,该URL仅在短时间内(例如1或2分钟)有效.
  • 营销站点将该URL作为302重定向发送给客户端.
  • 客户端浏览器重定向到业务站点.
  • 企业站点会验证URL,以确保URL尚未过期.
  • 企业站点将浏览器重定向回重定向到营销URL.在此响应中包括所有身份验证Cookie.

如果发生验证错误(例如超时或无效参数),则业务站点应重定向到营销站点上的预定义URL.

If a validation error occurs (e.g. timeout, or invalid parameters) then the business site should redirect to a predefined URL on the marketing site.

这篇关于在一个站点上为另一个站点创建经过asp.net身份验证的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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