通过电子邮件正文中的超链接打开指定的ASP.NET MVC视图 [英] Open Specified ASP.NET MVC Views From Hyperlinks Inside Email Body

查看:63
本文介绍了通过电子邮件正文中的超链接打开指定的ASP.NET MVC视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是了解ASP.NET MVC.我有一个具有以下情况的网站:

I just learn about ASP.NET MVC. I hava a website with this following scenario:

Login -> Main Page (Index) -> Edit Page (Edit)

因此,在用户登录时,在 LoginController 中,它将重定向到主页,并从 MainPage 中编辑一条记录.

So, In LoginController when user login, it will redirect to main page and edit a record from MainPage.

每次通过ASP.NET MVC创建新记录时,系统都会向经理发送电子邮件.在电子邮件中,有一个超链接,该超链接将重定向管理器以编辑表单.但是首先,他需要登录,因为除非他登录,否则无法打开编辑表单.例如:

Everytime a new record is created through ASP.NET MVC, the system will send an email to manager. Within email message, there is a hyperlink that will redirect the manager to edit form. But first, he needs to login because the edit form cant be opened unless he login. Ex:

http://localhost:1212/Main/Edit/ID-001

我在 MainController 中添加了 Authorize Attribute .但这仅适用于主页.因此,即使我尚未登录,也可以打开 Edit Page .

I have add Authorize Attribute within MainController. But It's only work for Main Page. So I can open Edit Page even I am not login yet.

这是MainController:

Here is the MainController:

[Authorize]
public class MainController : Controller
{
    string connString = @"Data Source=DESKTOP-FSET3FF,1433; Initial Catalog=INOVA_Data; User Id=sa; Password=Copoe113";

    public ActionResult Index(string username)
    {
        if (Session["username"] != null)
        {
            string user = Session["username"].ToString();
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            string sqlQuery = @"select Animals, Gender from dbo.Animals where Pemilik = @user";

            //string h = x => x.
            SqlCommand cmd = new SqlCommand(sqlQuery, conn);
            cmd.Parameters.AddWithValue("@user", user);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            conn.Close();
            return View(dt);
        }
        else
        {
            return RedirectToAction("Login", "Login");
        }
    }

    public ActionResult Edit()
    {
        return View();
    }
}

第二个问题,上面我已经写了我的网站方案,即

The Second Question, Above I have write my website scenario, that is

Login-> MainPage (Index) -> EditPage (Edit)

基于电子邮件的超链接,如何使应用程序重定向到EditPage而不重定向到MainPage.

Based On Hyperlink On Email, How to make application Redirect to EditPage without redirect to MainPage.

Login -> EditPage (Edit)

已编辑的第二个问题

简而言之,当用户尝试直接访问编辑视图时,应用程序会将用户重定向到登录视图.当成功登录后,应用程序会将用户重定向到编辑视图".

In short, when user's trying to access edit view directly, the application will redirect user to login view. And when heelr login success , the application will redirect user to Edit View.

但是现在,成功登录后,系统会将用户重定向到主视图.登录后如何使应用程序重定向到编辑视图?

But now, when login success, the system will redirect the user to main view. How to make the application redirect to edit view after login ?

推荐答案

重要说明 :(基于@Tashi Comment,我添加了此说明)不必担心通过会话管理对整个应用程序进行身份验证和授权.

Important note : (Based on @Tashi Comment, I added this note) If you are use mvc basic application with admin panel than do not worry about the authentication and authorization for whole application with session management.

当我们显式使用应用程序的自定义项并且必须在每个控制器中实现时,这是我们需要的.不要使用直接控制器进行继承(即 MainController:Controller ),而应使用自定义控制器来检查身份验证.

This is we need when we explicitly use our customization of app and that has to be implement in every controller. Rather than use direct controller for inheritance i.e. MainController : Controller , use custom controller where you check authentication.

/*You have to inherit this basecontroller in every controller*/
public class MainController : BaseController
{
     your actionmethods
}

和BaseController一样

And BaseController like

public class BaseController : Controller
    {

        public BaseController()
        {
            if (string.IsNullOrEmpty(SessionManager.SiteUrl))
            {
                SessionManager.SiteUrl = ConfigurationManager.AppSettings["SiteUrl"].ToString();
            }
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (SessionManager.UserId == -1)
            {
                switch (filterContext.ActionDescriptor.ActionName.ToLower().Trim())
                {
                    case "addeditcontact":
                       ViewBag.hdnPopupLogout = "0";
                       return;
                    default:
                        filterContext.Result = new   RedirectResult(Url.Action("Login", "Home"));
                        break;
                }
            }
        }
}

添加另一个用于会话管理的属性类

Add another property class for session management

public class SessionManager
{        

    public static int UserId
    {
        get
        {
            if (HttpContext.Current.Session["UserId"] != null)
            {
                return Convert.ToInt32(HttpContext.Current.Session["UserId"]);
            }
            else return -1;
        }
        set
        {
            HttpContext.Current.Session["UserId"] = value;
        }
    }

    public static string UserName
    {
        get
        {
            if (HttpContext.Current.Session["UserName"] != null)
            {
                return Convert.ToString(HttpContext.Current.Session["UserName"]);
            }
            else return string.Empty;
        }
        set
        {
            HttpContext.Current.Session["UserName"] = value;
        }
    }

   //reset user detail and add your custom property 
     public static void SignOutUser()
    {

        UserId = -1;
        UserName = string.Empty;          

    }

}

public ActionResult Login()
    {
        if (SessionManager.UserId == -1)
        {
            HttpCookie cookie = Request.Cookies["Login"];// Request.Cookies["Login"];

            if (cookie != null)
            {
                ViewBag.hdnUserID = cookie.Values[0];
                ViewBag.hdnPassword = cookie.Values[1];
                ViewBag.hdnRemember = "true";
            }
            return View("Login");
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }

现在这已准备好您的体系结构,我将给出您的答案

Now this is your architecture ready, I will give your answer

  1. 如果没有用户,以上这些内容可以防止未经授权的访问无需身份验证.
  2. 现在第二个问题是单击超链接时重定向到编辑页面.为此,在定义超链接时,可以创建操作方法来重定向,也可以使用javascript/ajax方法(用于身份验证)来重定向页面.

您需要在网格末尾设计html.如下面的图片.

You need to design html at your end for grid. As this below image.

并且在html中单元格的最后td上方是这样

and above last td of cell in html render as this

<td width="25%" >
   <a title="Edit" style="cursor:pointer" onclick="popupContacts(-2147481891,false );">Edit</a>&nbsp;
   <a style="cursor:pointer" title="Associate With Client" onclick="popupAssociateClient(-2147481891 );">Associate With Client</a>&nbsp;
   <a style="cursor:pointer" title="Update Contacts" onclick="popupUpdateContacts(-2147481891 );">Update Contacts</a>&nbsp;<a style="cursor:pointer" title="Export VCF" onclick="ExportContacttoVcf(-2147481891 );">Export VCF</a>&nbsp;
</td>

对于js,请重定向到另一个页面,我们首先在该页面中检查用户是否具有适当的权限,否则将重定向到使用操作方法登录.

for js, redirect to another page, where we first check that user have proper rights else redirect to login in actionmethod.

 function popupContactsDetails(clientid, contype) {          
        window.location.href = URL + "Home/ShowEditContact?Id=" + clientid + ",contype=" + contype;
    }

或者您可以使用与之相同的功能(在我操纵要在此处发布的代码时,可能在代码中有些错误,但要了解其背后的概念)

OR You can use the same function as(may be some thing wrong in code as I maniputlate code to publish here, but understand the concept behind this)

function popupContactsDetails(clientid, contype) {
 $.ajax({
        url: URL + "Home/ShowEditContact?Id=" + clientid + ",contype=" +      contype,
        type: 'get',
        dataType: 'json',
        async: false,
        //  data: ko.toJSON(this),
        contentType: 'application/json',
        success: function (result) {
            if (result = null) {
                alert("you are access wrong page);
                window.location.href = URL + "Home/login;
            } else{
                window.location.href = URL + "Home/yourpageurl; 
            }

        }
    });
}

这篇关于通过电子邮件正文中的超链接打开指定的ASP.NET MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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