小编dri*_*u16的帖子

MVC角色授权

我正在尝试实现角色授权机制,该机制检查当前登录用户的角色,如果用户处于正确的角色,则允许他/她,否则显示错误视图.

问题是,当用户尝试访问控制器中的下面方法时,他确实进入RoleAuthorizationAttribute类并进行了验证,但是后来控制器中的方法没有被执行.

注意:用户具有客户端角色

控制器方法

[RoleAuthorization(Roles = "Client, Adminsitrator")]
    public ActionResult addToCart(int ProductID, string Quantity)
    {
        tempShoppingCart t = new tempShoppingCart();
        t.ProductID = ProductID;
        t.Username = User.Identity.Name;
        t.Quantity = Convert.ToInt16(Quantity);

        new OrdersService.OrdersClient().addToCart(t);
        ViewData["numberOfItemsInShoppingCart"] = new OrdersService.OrdersClient().getNoOfItemsInShoppingCart(User.Identity.Name);
        ViewData["totalPriceInSC"] = new OrdersService.OrdersClient().getTotalPriceOfItemsInSC(User.Identity.Name);
        return PartialView("quickShoppingCart", "Orders");
    }
Run Code Online (Sandbox Code Playgroud)

角色身份验证类

[System.AttributeUsage(System.AttributeTargets.All,AllowMultiple = false, Inherited = true)]
public sealed class RoleAuthorizationAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {


        List<String> requiredRoles = Roles.Split(Convert.ToChar(",")).ToList();

        List<Role> allRoles = new UsersService.UsersClient().GetUserRoles(filterContext.HttpContext.User.Identity.Name).ToList();


        bool Match = false;

        foreach (String s in …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc authorization asp.net-mvc-3

13
推荐指数
1
解决办法
5万
查看次数

将会话变量值与字符串进行比较

我将会话变量与字符串进行比较,以检查登录类型是否为管理员.

我正在使用的代码:

if (Session["loggedInUsername"] == null)
        {
            btnLogin.Text = "Sign In";
            lblWelcome.Text = "Welcome!";
            hypManageRestaurants.Enabled = false;
            hypManageReviews.Enabled = false;
            hypPostReviews.Enabled = false;

        }
        else
        {
            if (Session["loggedInUserType"] == "Administrator")
            {
                hypManageRestaurants.Enabled = true;
                hypManageReviews.Enabled = true;
                hypPostReviews.Enabled = true;
            }
            else
            {
                hypManageRestaurants.Enabled = false;
                hypManageReviews.Enabled = false;
                hypPostReviews.Enabled = true;
            }
            lblWelcome.Text = "Welcome " + Session["loggedInUsername"];

            btnLogin.Text = "Sign Out";
        }
Run Code Online (Sandbox Code Playgroud)

所以首先我要检查是否有用户登录过.如果用户成功登录,则会话变量"loggedInUsername"将具有用户名的值.如果"loggedInUsername"会话变量不为空,它将检查"loggedInUserType"会话变量以获取登录用户的类型.

这里谈到的奇怪的事情,在"loggedInUserType"的价值是完全"管理员"不"",在那里我的会话变量与字符串IF函数"管理员"正被跳过,转到别的.

用户登录时,所有会话变量都会获取值.

以下是我用于登录的数据,此记录是唯一具有"管理员"类型的记录.

链接到图像

是否有任何其他方法可以将会话变量与字符串进行比较

c# asp.net session-variables

5
推荐指数
2
解决办法
2万
查看次数

IText7 html2pdf 使用 CSS 固定页脚

我目前正在尝试使用 itext7 和 itext7.pdfhtml 将 HTML 转换为 PDF,但遇到了一个小问题。

我有一个固定的页脚 (.footer),在使用浏览器打开时效果很好,但在使用下面的代码进行转换时,div 并未固定到页面底部。该 div 位于其之前的其他 div 内容之后。

C#.net核心代码

string fullBody = System.IO.File.ReadAllText("index.html"); 
var stream = new MemoryStream();
var writer = new iText.Kernel.Pdf.PdfWriter(stream);
writer.SetCloseStream(false); 
iText.Html2pdf.HtmlConverter.ConvertToPdf(fullBody , writer);
writer.Close();
stream.Seek(0, SeekOrigin.Begin);
Run Code Online (Sandbox Code Playgroud)

超文本标记语言

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="styles.css"> 
</head>
<body> 
    <div class="header">
        <img src="header1.bmp" width="100%" />
        <img src="header2.bmp" width="100%"/>
    </div>
    ... 
    <div class="footer">
        Fixed footer
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

CSS

.footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;  
    text-align: center;
} …
Run Code Online (Sandbox Code Playgroud)

html css itext html-to-pdf itext7

4
推荐指数
1
解决办法
3500
查看次数