Dave's Technophorical Times

A blog about Microsoft's Technologies!
SharePoint :: MVC :: ASP.NET :: IIS :: SQL Server :: Visual Studio :: MS Access

Problem:

I want to use email (or a dot in the username!) as the username with Microsoft.AspNet.Identity in MVC 5 application. But when I do I get the following error:

User name ... is invalid, can only contain letters or digits.

Solution:

In the AccountController constructor that takes a userManager as a parameter add the following code:

UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager)
 { AllowOnlyAlphanumericUserNames = false };


I had this issue a few times already, so I will write down the procedure I follow also as a reminder for myself:

  1. Replace all of the old solution name with the new one.
  2. Navigate to Properties under each project and change the Assembly Name and Default Namespace fields to new solution name.
  3. Go to solution folder and rename all project folders with the new solution name.
  4. Remove all files under bin and obj folders.
  5. Solution will not be able to load projects. Remove all projects and add them again.
  6. Re-build the project.

Source: http://stackoverflow.com/questions/21701297/error-when-renaming-asp-net-mvc-5-applciation



When developing your Excel or Access application, it’s not uncommon to need to capture your user’s domain/username for one reason or another. A user’s domain/username combination is the traditional unique identifier for a user - often captured by programmers to do things in like:

  • Help manage user login and permissions
  • Create a history of who used the application
  • Capture the name of each user who made the changes to data
  • Personalize outputs and program messages for users

.
There are literally half a dozen ways to capture a user’s domain name and user name. In this post, I’ll give you a very easy method using the Environ function.

Environ stands for Environment, and refers to environment variables in an operating system. Environment variables are specially named aliases for specific system properties, exposed as a kind of shortcut for system administrators and programmers.

Although majority of these Environment variables are useless to your average Excel/Access developer. There are a few that can prove to be useful. Two of these are the UserDomain and UserName variables. We can use these to capture a user’s Domain\UserName.

To demonstrate this, I’ve entered the following function into a standard module. You can call this from a form or query!

Function getReturnUName()
       getReturnUName = Environ(“UserDomain”) & “\” & Environ(“Username”)
End Function

  •  

  • Sub EnvironListing()
  •  Dim I As Integer
  •  I = 1
  •   Do Until environ(I) = ""
  •     Debug.Print environ(I)
  •     I = I + 1
  •   Loop
  • End Sub


  • Sometimes you need to refresh the local cache in SQL Server when writting code to remove that annoying red squiggly line.

    Edit -> Intellisense - Refresh Local Cache
    OR
    Ctrl-Shift-R

    Enjoy
    Dave



    Usually you will see this if the user does not have rights to the SharePoint config database, or if the .NET 4.X framework is not installed.

    All the Best
    Dave



    Here is a script that I wrote for backing up some very important files to an off-site ftp location. It backs up only files that are new OR have changed in the last 24 hours. That number of days could be a parameter based on when the backup last ran, however this is the base code that you can start using right away. Just add it to a Task Schedule that runs one a day.


    # ==============================================================================================
    #Set the Date/Time
    # ==============================================================================================
    $BackUpdateTime  = (Get-Date).Year.ToString()
    $BackUpdateTime += (Get-Date).Month.ToString()
    $BackUpdateTime += (Get-Date).Day.ToString()
    $BackUpdateTime += (Get-Date).Hour.ToString()
    $BackUpdateTime += (Get-Date).Minute.ToString()
    $BackUpdateTime += (Get-Date).Second.ToString()
    $today = (Get-Date -Format yyyy-MM-dd)

    try {
     $ftp = "ftp://ftp.mysite.ca/"
     $user = "ftpuser"
     $pass = "ftppassword" 
     
     $webclient = New-Object System.Net.WebClient
     $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 

     #we specify the directory where all files that we want to upload 
     $Dir="Y:\LocalDirectory\"
     $LogFile="I:\PowerShell\MyBackup_"+$today+".txt"
     Clear-Host
     #Write-Host $LogFile
     "From:"+$Dir+" (on server01) To:"+$ftp | Out-File $LogFile -Append
     "Start: "+(Get-Date) | Out-File $LogFile -Append

     $files = @(Get-ChildItem -Path  $Dir -Recurse | ?{ !$_.PSIsContainer } |Where-Object { $_.lastwritetime -gt (get-date).AddDays(-1)} | Select-Object -ExpandProperty FullName )
     foreach($item in $files)
     {
         if($item -ne $null)
      {
       $uri = New-Object System.Uri($ftp+$item.Substring(3))
       $webclient.UploadFile($uri, $item)
       #Write-Host (Get-Date)$item
       "$(Get-Date): "+$item | Out-File $LogFile -Append
      }
     }
     $webclient.Dispose()

     "End:"+(Get-Date) | Out-File $LogFile -Append
     
     $msg = new-object Net.Mail.MailMessage
     
     # Edit the From Address as per your environment. 
     $msg.From = "Backup (server01) <my.email@mysite.ca>"

     # Edit the mail address to which the Notification should be sent. 
     $msg.To.Add("my.email@mysite.ca")

     # Subject for the notification email. The + “$today” part will add the date in the subject. 
     $msg.Subject = "Backup was Successful for " + "$today"

     # Body or the notification email. The + “$today” part will add the date in the subject. 
     $msg.Body = "Backup was Successful for " + $today + "`r`n`r`n" 
     
     $att = new-object Net.Mail.Attachment($LogFile)
     $msg.Attachments.Add($att)
     
     # IP address of your SMTP server.
     $smtpServer = "smtp.mysite.ca" 
     $smtp = new-object Net.Mail.SmtpClient($smtpServer) 

     $smtp.Send($msg)
     $msg.Dispose()
    }
    Catch { 
     $ErrorMessage = $_.Exception.Message 

     # Configure the below parameters as per the above. 
     $msg = new-object Net.Mail.MailMessage
     $msg.From = "Backup (server01) <my.email@mysite.ca>" 
     $msg.To.Add("my.email@mysite.ca") 
     $msg.Subject = "Backup Job failed on " + "$today" 
     $msg.Body = "Job failed on " + "$today and the reason for failure was $ErrorMessage." 
     
     $att = new-object Net.Mail.Attachment($LogFile)
     $msg.Attachments.Add($att)
     
     $smtpServer = "smtp.mysite.ca" 
     $smtp = new-object Net.Mail.SmtpClient($smtpServer) 
     
     $smtp.Send($msg)
     $msg.Dispose()
    }



    The files that you attach to an email become locked until the instance of Powershell you are running has exited completely. So if you run a script through Powershell ISE that attaches a file to an email, that file will remain locked until you exit Powershell ISE.

    If the file is locked by Powershell, you will get an error/warning message similar to the following if you try to modify it in any way;

    The process cannot access the file 'c:\filename.txt' because it is being used by another process

    By using the following command, you can ensure that Powershell 'disposes' of the email message once it has been sent and does not continue to 'lock' any files you attach and send via email;

    $mailmessage.dispose()

    Note: this is assuming that $MailMessage = New-Object system.net.mail.mailmessage



    Here is some code for exporting your Outlook Notes.

    Sub NotesToRTF()
        Dim myNote As Variant
        Dim cnt As Integer
        Dim noteName As String
        Dim strExportFolder As String
       
        strExportFolder = "C:\Notes-RTF\"
       
        If Dir(strExportFolder, vbDirectory) = "" Then
            MkDir strExportFolder
        End If
       
        Set myNote = Application.GetNamespace("MAPI").PickFolder
        For cnt = 1 To myNote.Items.Count
            noteName = Trim(Replace(Replace(Replace(Replace(myNote.Items(cnt).Subject, "/", "-"), "\", "-"), ":", ""), vbTab, ""))
            Debug.Print noteName
            myNote.Items(cnt).SaveAs strExportFolder & noteName & ".rtf", OlSaveAsType.olRTF
        Next
       
        Shell "C:\WINDOWS\explorer.exe """ & strExportFolder & "", vbNormalFocus
    End Sub

    Sub NotesToText()
        Dim myNote As Variant
        Dim cnt As Integer
        Dim noteName As String
        Dim strExportFolder As String
       
        strExportFolder = "C:\Notes-Text\"
       
        If Dir(strExportFolder, vbDirectory) = "" Then
            MkDir strExportFolder
        End If
       
        Set myNote = Application.GetNamespace("MAPI").PickFolder
        For cnt = 1 To myNote.Items.Count
            noteName = Trim(Replace(Replace(Replace(Replace(myNote.Items(cnt).Subject, "/", "-"), "\", "-"), ":", ""), vbTab, ""))
            Debug.Print noteName
            myNote.Items(cnt).SaveAs strExportFolder & noteName & ".txt", OlSaveAsType.olTXT
        Next
       
        Shell "C:\WINDOWS\explorer.exe """ & strExportFolder & "", vbNormalFocus
    End Sub

    Hope it helps someone else out there.



    When you install a new farm, you might be curious to find out what the Health Analyzer results are. Instead of waiting for the checks to happen (basically timer jobs scheduled at different intervals), you could force them to run all at one go, so you can review the results and address them.

    You can easily run this in PowerShell. All it does is start all the timer jobs with ‘Health’ in its name. That fires off all SharePoint 2010 Health Analyzer jobs for your new farm in one go.

    # Check to ensure Microsoft.SharePoint.PowerShell is loaded
    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
    if ($snapin -eq $null) {
      Write-Host "Loading SharePoint Powershell Snapin"
      Add-PSSnapin "Microsoft.SharePoint.Powershell"
    }
    Get-SPTimerJob | Where {$_.Name -like "*Health*" -and $_.Name -like "*-all-*"} | Start-SPTimerJob


    QUESTION: I'm using the SharePoint 2010 Health Analyzer and I've noticed that on some of the issues, when I open the Review window either or both of the Actions or Reanalyze Now options are grayed out. The pop up window says I may not have the right permission level to use this, might need to select an dobject or item, or the control might now work in this context. Can anyone explain why I wouldn't be able to do a reanalyze now? It seems strange this would be grayed out at all.

    ANSWER:

    Not only was the reanalyze greyed out but non of the scheduled tasks were running. I was able to trace it down to the account that was used to run "SharePoint 2010 Timer" service was not a local admin. I changed it to an account that is a domain service account that has local admin rights and had the rights outlined below:

    1.Verify that the user account that is performing this procedure is a member of the Administrators group on the local computer.

    2.Click Start, click Administrative Tools, and then click Services.

    3.Right-click Windows SharePoint Services Timer V4, and then click Properties.

    4.On the Log On tab, confirm that the account being used is a domain user account and is a member of the following:

    dbcreator fixed SQL Server server role
    securityadmin fixed SQL Server server role
    db_owner fixed database role for all databases in the server farm

     Here is the Microsoft link I used.

    http://technet.microsoft.com/en-us/library/ee924649.aspx

    This resolved my issue. I hope this helps.



    The Blogger

    Dave Stuart I'm a Developer with a passion for coding. I enjoy the challengers that come with the job! SharePoint is one of my expert areas along with .NET Web Development with MVC and good old MS Access VBA coding. I Blog so that I can remember how I did that way back when; PLUS all this stuff is searchable! I constantly study and run my own business, Dafran Inc. I have passed 22 Microsoft Exams since 1998 when I first jumped on the treadmill of knowledge. I hope that you enjoy this Blog as much as I enjoy updating it. All the very best from Calgary, Alberta, Canada. contact me at linkedin @ dafran.ca

    Calendar

    <<  September 2019  >>
    MoTuWeThFrSaSu
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    30123456

    View posts in large calendar

    Sign in