我有一个使用ASP.NET MVC 5设计的网站,其网站名称为www.oldsite.com
。
我们刚刚启动了一个新网站-www.newsite.com
,对ASP.NET MVC代码进行了一些更改,但是两个站点的数据库相同。
当用户登录到旧网站时,www.oldsite.com
会验证登录详细信息(用户名和密码),并在成功登录后根据某种条件将用户重定向到具有自动登录功能的新网站www.newsite.com
(用户无需在www.newsite.com
的登录页面上再次输入用户ID和密码,并显示www.newsite.com
的主页。
这是我的代码
int timeout = login.RememberMe ? 600 : 60; // 525600 min = 1 year
var ticket = new FormsAuthenticationTicket(v.PEmailId, login.RememberMe, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = DateTime.Now.AddMinutes(timeout);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
if (some condition)
{
return Redirect("www.newsite.com");
}
我需要一些登录身份验证cookie代码,我正在使用ASP.NET身份。
请告诉我如何使用登录凭据(在登录页面中提供userid和password参数并自动登录到新网站)从旧网站重定向到新网站www.newsite.com
或如何为新网站{{ 1}},无需输入用户名和密码即可自动登录。
谢谢
答案 0 :(得分:0)
您可以从旧站点传递用户名和密码作为参数。
return Redirect(string.Format("https://newsite.com/Home/Index?username={0}&password={1}","username", "password"));
在新站点中,创建一个允许匿名用户使用的功能。然后验证用户凭证。如果用户有效,则添加cookie并重定向到所需的页面。
[AllowAnonymous]
public class HomeController : Controller
{
public ActionResult Index(string username, string password)
{
// validate the user credential
// add cookie
User user = new User();
user.UserName = username;
user.Password = password;
AddCookie(user);
return RedirectToAction("Index", "Dashboard");
}
public void AddCookie(User user)
{
string encryptTicket, userData;
HttpCookie httpCookie = null;
try
{
userData = JsonConvert.SerializeObject(user);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, user.UserName, DateTime.Now, DateTime.Now.AddHours(1), true, userData, FormsAuthentication.FormsCookiePath);
encryptTicket = FormsAuthentication.Encrypt(ticket);
httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptTicket);
Response.Cookies.Add(httpCookie);
}
catch (Exception exception)
{
}
return httpCookie;
}
}
public class User
{
public string UserName { get; set; }
public string Password { get; set; }
}