首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mvc,授权反弹授权用户。

Mvc,授权反弹授权用户。
EN

Stack Overflow用户
提问于 2016-05-27 12:38:57
回答 1查看 262关注 0票数 4

我试图使MVC 5网页中的一部分仅限于某个活动目录组的用户,但是也有登录到用户中的授权属性(on控制器)块。

后面的登录页代码如下所示:

代码语言:javascript
复制
public class AccountController: Controller
{

    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            ActiveDirectoryHelper ad = new ActiveDirectoryHelper();

            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                if (ad.CheckGroupMembership(model.UserName))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Credentials are correct but you are no authorised \n You Need membership in group: HKF-HIT-FortigateAPI-GS");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect");
            }
        }
        // if we got this far, something failed, redisplay form
        return View(model);
    }
    // POST: /Account/LogOff
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home");
    }
}
public class ActiveDirectoryHelper
{
    string group = "HKF-HIT-FortigateAPI-GS";
     public bool CheckGroupMembership(string name)
    {
        var context = new PrincipalContext(
                            ContextType.Domain,
                            "AD-Domain", @"Username", "Password");

        var userPrincipal = UserPrincipal.FindByIdentity(
                            context,
                            IdentityType.SamAccountName,
                            name);

        var test = userPrincipal;

        if (userPrincipal.IsMemberOf(context,
             IdentityType.Name,
             group))
        {
            return true;
        }
        return false;
    }
}

用户通过并重定向到家庭控制器中的索引。

但是,此控制器的授权值设置如下:

代码语言:javascript
复制
[Authorize]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

在这里,用户在弹回登录页,好像他没有得到授权。

这也是web.config:

在浏览器中,我可以看到ADAuthCookie。

编辑: Ading图片的请求数据:

账户邮政:

费德勒:

索引获取:

费德勒:

编辑:问题已经解决了,在浏览了由评论链接到的神奇指南之后,我意识到我从来没有在Global.asaz.cs课堂上处理过我的库克。

在Application_PostAuthenticateRequest中添加一个overide解决了我的问题。

我添加的代码最后使用:

代码语言:javascript
复制
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

    if (authCookie != null)
    {
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

        JavaScriptSerializer serializer = new JavaScriptSerializer();

        CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData);

        CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
        newUser.Name = serializeModel.Name;
        HttpContext.Current.User = newUser;
    }
}

在global.asax中,我还添加了:

代码语言:javascript
复制
CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
serializeModel.Name = model.UserName;

JavaScriptSerializer serializer = new JavaScriptSerializer();

string userData = serializer.Serialize(serializeModel);

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
         1,
         model.UserName,
         DateTime.Now,
         DateTime.Now.AddMinutes(15),
         false,
         userData);

string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
Response.Cookies.Add(faCookie);

到我的登录页面。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-27 13:28:50

AuthorizeAttribute implementation).

所有安全框架(身份、成员等)微软使用这些接口与MVC/ASP.NET进行通信。如果您使用的是自定义安全框架,则还需要实现这些接口,并在AcquireRequestState (如果使用会话状态)或PostAuthorizeRequest事件中设置它们。

有关后者的示例,请参阅ASP.NET MVC - Set custom IIdentity or IPrincipal,以及自定义IPrincipalIIdentity实现。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37483817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档